Ritu

Ritu

  • 1.3k
  • 344
  • 37.7k

map json values to class object

Feb 9 2024 7:33 AM

Hi I need help below is the json where I have to read and create csv file as output.

{
    "COMP_DUR": "100",
    "DATE": "2024-01-10T16:38:00Z",
    "EMAIL": "[email protected]",
    "ID": "1111",    
    "approval": "Medium",
    "criteria": "Medium",
    "access": "Neutral"    
}

highlighted in yellow colour are json key and values but in green colour are the dynamic json values which i have to map with json key(REQ and RES)

I am doing this as follows

JObject convertedJson = JObject.Parse(json);
IDictionary<string, string> quesRespkeyValueParam = new Dictionary<string, string>();
foreach (var item in convertedJson.Properties())
{
    //Console.WriteLine(item.Name + "," + item.Value); 

    if (item.Name == "SURVEY_ID" || item.Name == "RESPONDENT_ID" || item.Name == "RESPONDENT_NAME" || item.Name == "RESPONDENT_EMAIL" ||
        item.Name == "DATE_RESPONDED" || item.Name == "COMPLETION_DURATION" || item.Name == "SURVEY_URL")
    {
        JsonSerializerOptions options = new()
        {
            DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault
        };
    }
    else
    {
       quesRespkeyValueParam.Add(item.Name, (string)item.Value);
       tempObj.QuesResp = (Dictionary<string, string>)quesRespkeyValueParam;     
    }
}
obj = tempObj;

I am able to add values in IDictionary object but not able to map with json keys REQ and RES

COMP_DUR DATE EMAIL ID REQ RES
100 2024-01-10T16:38:00Z [email protected] 1111 approval Medium
100 2024-01-10T16:38:00Z [email protected] 1111 criteria Medium
100 2024-01-10T16:38:00Z [email protected] 1111 access Neutral

 please help. Thanks


Answers (1)