Extracting JsonObject from JsonArray

Refresh

November 2018

Views

1.9k time

1

I have a problem to extract a JasonObject from a Arry, that contains some objects without a name. Ive got the Array by a batch request form graph.facebook.

[
    {
    "code":200,
    "headers":[{...}],
    "body":"{
        \"id\":\"255572697884115_1\",
        \"from\":{
            \"name\":\"xyzk\",
            \"id\":\"59788447049\"},
        \"message\":\"Hey\",
        \"created_time\":\"2011-11-04T21:32:50+0000\"}"},
    {
    "code":200,
    "headers":[{...}],
    "body":"{
        \"id\":\"255572697884115_2\",
        \"from\":{
             \"name\":\"xyzk\",
             \"id\":\"59788447049\"},
        \"message\":\":P\",
        \"created_time\":\"2012-01-03T21:05:59+0000\"}"}
]

Now I have to read the vaules "message" of the containing objects, but i dont know how i can access the objects in the array. Can anybody give me a helping hand?

I want to use System.Json, additional Newtonsoft.Json.

In Java it is easy to use by GetJsonObject(), but how can i succeed with VSC#? There is an method JsonValueLinqExtensions.ToJsonObject but i dont know, how to use. Could sb give me an example?

Thank you so far,

Dominic

1 answers

2

You could parse the JSON into a dynamic using JavaScriptSerializer object e.g.

var serializer = new JavaScriptSerializer();   
var result = serializer.Deserialize<dynamic>(json);
foreach (var item in result)
{
    Console.WriteLine(item["body"]["message"]);
}