1

How do I parse the following json data using JSON.net lib

    Root: {
  "data": [
    {
      "name": "query1",
      "fql_result_set": [
        {
          "thread_id": "1920370693067",
          "updated_time": 1340656102,
          "subject": "",
          "snippet": "Test",
          "snippet_author": 100002153560476
        }
      ]
    },
    {
      "name": "query2",
      "fql_result_set": [
        {
          "uid": 100002153560476,
          "name": "Santosh Singh"
        }
      ]
    }
  ]
}
    Type: Property

I tried following code,but not able to get data

JObject j = JObject.Parse(jsonResult);
JArray data = (JArray)j["data"];

if (data != null)
        {
            foreach (var item in data[1]["fql_result_set"].Values())
            {

                string innerText = item["name"].ToString();

                string str2 = item["uid"].ToString();
                dictionary[str2] = innerText;
            }
            foreach (var item in data[0]["fql_result_set"].Values().AsJEnumerable())
            {
                FacebookMessage message;
                message.threadId = item["thread_id"].ToString();
                message.updatedTime = item["updated_time"].ToString();
                message.subject = item["subject"].ToString();
                message.snippet = item["snippet"].ToString();
                message.snippetAuthorId = item["snippet_author"].ToString();
                message.snippetAuthorName = dictionary[message.snippetAuthorId];
                allMessages.Add(message);
            }
}

1 Answer 1

2

First of all your json string is not complete. I tried to convert it to a valid one as:

{
    "Root": {
        "data": [
            {
                "name": "query1",
                "fql_result_set": [
                    {
                        "thread_id": "1920370693067",
                        "updated_time": 1340656102,
                        "subject": "",
                        "snippet": "Test",
                        "snippet_author": 100002153560476
                    }
                ]
            },
            {
                "name": "query2",
                "fql_result_set": [
                    {
                        "uid": 100002153560476,
                        "name": "Santosh Singh"
                    }
                ]
            }
        ]
    }
}

enter image description here

And parsed as:

dynamic dynObj = JsonConvert.DeserializeObject(json);
foreach(var data in dynObj.Root.data)
{
    Console.WriteLine("{0}",data.name);
    foreach(var fql in data.fql_result_set)
    {
        foreach (JProperty keyValue in fql)
        {
            Console.WriteLine("\t{0} : {1}", keyValue.Name,keyValue.Value);
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I am getting null refrence exception on line foreach(var data in dynObj.Root.data)
@geek I showed you the json I tried to parse. The json string you posted is invalid. What does Type: Property mean for example? Where is the first { ? . Check your json string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.