1

I am trying for many hours to parse a JsonArray, I have got by graph.facebook, so that i can extra values. The values I want to extract are message and ID.

Getting the JasonArry is no Problem and works fine:

[
    {
    "code":200,
    "headers":[{"name":"Access-Control-Allow-Origin","value":"*"}],
    "body":"{
        \"id\":\"255572697884115_1\",
        \"from\":{
            \"name\":\"xyzk\",
            \"id\":\"59788447049\"},
        \"message\":\"This is the first message\",
        \"created_time\":\"2011-11-04T21:32:50+0000\"}"},
    {
    "code":200,
    "headers":[{"name":"Access-Control-Allow-Origin","value":"*"}],
    "body":"{
        \"id\":\"255572697884115_2\",
        \"from\":{
             \"name\":\"xyzk\",
             \"id\":\"59788447049\"},
        \"message\":\"This is the second message\",
        \"created_time\":\"2012-01-03T21:05:59+0000\"}"}
]

Now I have tried several methods to get access to message, but every method ends in catch... and throws an exception.

For example:

var serializer = new JavaScriptSerializer();   
var result = serializer.Deserialize<dynamic>(json);

foreach (var item in result)
{
    Console.WriteLine(item.body.message);
}

throws the exception: System.Collections.Generic.Dictionary doesnt contain definitions for body. Nevertheless you see in the screenshot below, that body contains definitions.

Becaus I am not allowed to post pictures you can find it on directupload: http://s7.directupload.net/images/120907/zh5xyy2k.png

I don't havent more ideas so i please you to help me. I need this for a project, private, not commercial.

Maybe you could give me an phrase of code, so i can continue my development.

Thank you so far

Dominic

Findus
  • 303
  • 1
  • 4
  • 17

3 Answers3

5

If you use Json.Net, All you have to do is

replacing

var serializer = new JavaScriptSerializer();
var result = serializer.Deserialize<dynamic>(json);

with

dynamic result = JsonConvert.DeserializeObject(json);

that's all.

L.B
  • 114,136
  • 19
  • 178
  • 224
3

You are not deserializing to a strongly typed object so it's normal that the applications throws an exception. In other words, the deserializer won't create an Anynymous class for you.

Your string is actually deserialized to 2 objects, each containing Dictionary<string,object> elements. So what you need to do is this:

var serializer = new JavaScriptSerializer();   
var result = serializer.Deserialize<dynamic>(s);

foreach(var item in result)
{
    Console.WriteLine(item["body"]["message"]);
}

Here's a complete sample code:

void Main()
{
        string json =  @"[
    {
    ""code"":200,
    ""headers"":[{""name"":""Access-Control-Allow-Origin"",""value"":""*""}],
    ""body"":{
        ""id"":""255572697884115_1"",
        ""from"":{
            ""name"":""xyzk"",
            ""id"":""59788447049""},
        ""message"":""This is the first message"",
        ""created_time"":""2011-11-04T21:32:50+0000""}},
    {
    ""code"":200,
    ""headers"":[{""name"":""Access-Control-Allow-Origin"",""value"":""*""}],
    ""body"":{
        ""id"":""255572697884115_2"",
        ""from"":{
             ""name"":""xyzk"",
             ""id"":""59788447049""},
        ""message"":""This is the second message"",
        ""created_time"":""2012-01-03T21:05:59+0000""}}
]";
    var serializer = new JavaScriptSerializer();   
    var result = serializer.Deserialize<dynamic>(json);

    foreach(var item in result)
    {
        Console.WriteLine(item["body"]["message"]);
    }

}

Prints:

This is the first message
This is the second message
Icarus
  • 63,293
  • 14
  • 100
  • 115
  • Thank you for your very fast answer. You are right, your code is working, but you manually delete quotes < "body":"{> --> <""body"":{>. I get the string (in fact a stream) from a webclient. Isn't there any chance to deserialize without manipulating? – Findus Sep 07 '12 at 07:37
  • @DominicFrank I thought you had added those quotes yourself by accident since the whole string is obviously manipulated. String as you posted it is not valid JSON. Go to http://jsonlint.com and paste it there. I also tried running the code with the exact string you posted an it doesn't even pass the Deserialize line since the JavaScriptSerializer throws an exception due to the invalid JSON string. At any rate, the mistake you are doing is expecting that the Serializer is going to create a Strongly Typed object. It WONN'T unless you create a class with the exact same structure and ... Continued – Icarus Sep 07 '12 at 10:28
  • @DominicFrank you tell the Serializer to Deserialize to that type. See here for an example: http://stackoverflow.com/questions/7895105/json-deserialize-c-sharp – Icarus Sep 07 '12 at 10:31
1

I am using this simple technique

 var responseTextFacebook = 
 @"{
       "id":"100000891948867",
       "name":"Nishant Sharma",
       "first_name":"Nishant",
       "last_name":"Sharma",
       "link":"https:\/\/www.facebook.com\/profile.php?id=100000891948867",
       "gender":"male",
       "email":"nihantanu2010\u0040gmail.com",
       "timezone":5.5,
       "locale":"en_US",
       "verified":true,
       "updated_time":"2013-06-10T07:56:39+0000"
    }"

I have declared a class

    public class RootObject
    {
        public string id { get; set; }
        public string name { get; set; }
        public string first_name { get; set; }
        public string last_name { get; set; }
        public string link { get; set; }
        public string gender { get; set; }
        public string email { get; set; }
        public double timezone { get; set; }
        public string locale { get; set; }
        public bool verified { get; set; }
        public string updated_time { get; set; }
    }

Now I am deserializing

JavaScriptSerializer objJavaScriptSerializer = new JavaScriptSerializer();
RootObject parsedData = objJavaScriptSerializer.Deserialize<RootObject>(responseTextFacebook );
Nishant Kumar
  • 5,995
  • 19
  • 69
  • 95