Parsing Json facebook c#

2019-08-03 05:14发布

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

3条回答
【Aperson】
2楼-- · 2019-08-03 05:46

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 );
查看更多
手持菜刀,她持情操
3楼-- · 2019-08-03 05:54

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
查看更多
爷、活的狠高调
4楼-- · 2019-08-03 06:07

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.

查看更多
登录 后发表回答