Gokul Kumar
Gokul Kumar

Reputation: 409

How to get values from dynamic array?

I have deserialized the string by using the dynamic object.

Because property names dynamically changed. so can't able to deserialize with DTO objects.

Please find the code snippet:

var result = "[{\"series1\":\"{\\\"Category1\\\":4.3,\\\"Category2\\\":2.5,\\\"Category3\\\":3.5}\"},{\"series2\":\"{\\\"Category1\\\":2.4,\\\"Category2\\\":4.4,\\\"Category3\\\":1.8}\"},{\"series3\":\"{\\\"Category1\\\":2,\\\"Category2\\\":2,\\\"Category3\\\":3}\"}]");

var jsonResult = JsonConvert.DeserializeObject<dynamic[]>(jsonText);

enter image description here

How to retrieve the series1 and category1, 2, 3 results?

Could you please help me to solve this?

Upvotes: 1

Views: 1042

Answers (3)

Brett Caswell
Brett Caswell

Reputation: 1504

As @Jamiec mentioned, there is inline JSON string to consider; so, you will have to deserialize twice.

For this answer, we'll avoid using dynamic altogether (without creating a custom JsonConverter) and avoid JObject (i.e. IDictionary<string, JToken>);


We'll define a Type for Categories. (For the second, iterative Deserialization)

    public class Categories
    {
        public double Category1 { get; set; }
        public double Category2 { get; set; }
        public double Category3 { get; set; }
    }

We'll replace dynamic[] with IEnumerable<IDictionary<string, string>>. Each dictionary has only one entry actually, so we'll simply join the Keys and Values using string.Join() in our iteration handling.

    var jsonText = "[{\"series1\":\"{\\\"Category1\\\":4.3,\\\"Category2\\\":2.5,\\\"Category3\\\":3.5}\"},{\"series2\":\"{\\\"Category1\\\":2.4,\\\"Category2\\\":4.4,\\\"Category3\\\":1.8}\"},{\"series3\":\"{\\\"Category1\\\":2,\\\"Category2\\\":2,\\\"Category3\\\":3}\"}]";

    var jsonResult = JsonConvert.DeserializeObject<IEnumerable<IDictionary<string, string>>>(jsonText);

    var r = jsonResult.Select(i => 
    {
        (string Series, Categories Categories) result = (string.Join(string.Empty, i.Keys), JsonConvert.DeserializeObject<Categories>(string.Join(string.Empty, i.Values)));
        return result;
    }).ToArray();

(the last part uses a Deconstruct to ValueTuple)

dynamic here was actually a JObject which implements IDictionary<string, JToken>. JToken is fairly flexible (infact it works with the snippet above - including string.Join usage), but as a matter of scope and preference we deserialized to a primitive string.

Upvotes: 1

Jamiec
Jamiec

Reputation: 136154

The problem you will have is you have json stuffed within json - obvious from the fact that you have double escape sequences there. So to get series1 is fairly straightforward:

var jsonResult = JsonConvert.DeserializeObject<dynamic[]>(result);
Console.WriteLine(jsonResult[0]["series1"]);

But that itself is another json string. So you'll need to then parse that again to get Category1 etc:

var jsonResult = JsonConvert.DeserializeObject<dynamic[]>(result);
var series1Result = JsonConvert.DeserializeObject<dynamic>(jsonResult[0]["series1"].ToString());
Console.WriteLine(series1Result["Category1"]); // 4.3

Live example: https://dotnetfiddle.net/hcACrM

Upvotes: 2

Peter Marshall
Peter Marshall

Reputation: 1355

Try jsonResult["series1"]["Category"]

Upvotes: 0

Related Questions