Edlaos
Edlaos

Reputation: 13

how can i get nested data from a JsonConvert.DeserializeObject<dynamic>(example)

Using the example code works correctly to get the non-nested data from "issues", how can I get the nested data from "issues"? In the picture you can see which data we are talking about.

Code:

try
{
   var dynObj = JsonConvert.DeserializeObject<dynamic>(strResponseValue);
   string records = dynObj["issues"].ToString();
   JArray a = JArray.Parse(records);
   List<string> colNames = new List<string>() {"expand", "key", "id", 
   "self" };

   string headerRow = "";
   foreach (string colName in colNames)
   {
      headerRow += "\"" + colName + "\";";
   }
   headerRow = headerRow.TrimEnd(';');
   headerRow += "\n";

   string dataRows = "";
   foreach (var record in a)
   {
      string thisRecord = "";
      foreach (string colName in colNames)
      {
         thisRecord += "\"" + record[colName] + "\";";
      }
      thisRecord = thisRecord.TrimEnd(';');
      thisRecord += "\n";
      dataRows += thisRecord;
   }
   csvData = headerRow + dataRows;

   Console.WriteLine("\ncsvData:");
   Console.WriteLine(csvData);
}
catch (Exception ex)
{ 
   Console.WriteLine("Exeption Error" + ex.ToString()); 
}

enter image description here

Upvotes: 1

Views: 51

Answers (1)

Kraego
Kraego

Reputation: 3902

If the json is complex, you can let visual studio do the job.

  1. Copy the json content
  2. Use Edit/Paste Special - Paste JSON As Classes
  3. Use the generated Class to deserialize your json content.

For instance deserialize to MyClass (from .Net Fundumentals - JSON Serialize/Deserialize):

MyClass? myclass = JsonSerializer.Deserialize<MyClass>(jsonString);

Upvotes: 1

Related Questions