1

我尝试了很多小时来解析一个 JsonArray,我已经通过 graph.facebook 获得,所以我可以额外的值。我要提取的值是消息和 ID。

获得 JasonArry 没问题,并且工作正常:

[
    {
    "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\"}"}
]

现在我尝试了几种方法来访问消息,但是每个方法都以 catch... 结尾并引发异常。

例如:

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

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

引发异常: System.Collections.Generic.Dictionary 不包含正文的定义。尽管如此,您在下面的屏幕截图中看到,该正文包含定义。

因为我不允许发图片你可以在directupload上找到:http: //s7.directupload.net/images/120907/zh5xyy2k.png

我没有更多的想法,所以我请你帮助我。我需要这个项目,私人的,而不是商业的。

也许你可以给我一段代码,这样我就可以继续我的开发了。

谢谢你到目前为止

多米尼克

4

3 回答 3

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.

于 2012-09-07T09:48:44.097 回答
3

您没有反序列化为强类型对象,因此应用程序抛出异常是正常的。换句话说,反序列化器不会为您创建 Anynymous 类。

您的字符串实际上被反序列化为 2 个对象,每个对象都包含Dictionary<string,object>元素。所以你需要做的是:

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

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

这是一个完整的示例代码:

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"]);
    }

}

印刷:

This is the first message
This is the second message
于 2012-09-06T22:50:53.983 回答
1

我正在使用这种简单的技术

 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"
    }"

我已经宣布了一个班级

    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; }
    }

现在我正在反序列化

JavaScriptSerializer objJavaScriptSerializer = new JavaScriptSerializer();
RootObject parsedData = objJavaScriptSerializer.Deserialize<RootObject>(responseTextFacebook );
于 2013-07-20T08:44:58.000 回答