使用来自twitter的多个细节解析Json数据

时间:2014-01-29 12:36:13

标签: c# json twitter json.net

我从twitter获得以下输出,我想只从输出中捕获以下细节。

  1. 屏幕名称
  2. 追随者计数
  3. profile_image_url
  4. 问题是数据在嵌套的Json中。

    Twitter API输出:

    [{ "contributors" : null,
    "coordinates" : null,
    "created_at" : "Wed Jan 29 09:18:15 +0000 2014",
    "favorite_count" : 0,
    "favorited" : false,
    "geo" : null,
    "id" : 428457050109382657,
    "id_str" : "428457050109382657",
    "in_reply_to_screen_name" : null,
    "in_reply_to_status_id" : null,
    "in_reply_to_status_id_str" : null,
    "in_reply_to_user_id" : null,
    "in_reply_to_user_id_str" : null,
    "lang" : "en",
    "place" : null,
    "user" : { "contributors_enabled" : false,
        "follow_request_sent" : null,
        "followers_count" : 218,
        "id" : 609465835,
        "id_str" : "609465835",
        "is_translation_enabled" : false,
        "is_translator" : false,
        "lang" : "en",
        "listed_count" : 3,
        "location" : "Still alpha as hell.",
        "verified" : false
      }
    }, 
    { "contributors" : null,
    "coordinates" : null,
    "created_at" : "Wed Jan 29 09:18:15 +0000 2014",
    "favorite_count" : 0,
    "favorited" : false,
    "geo" : null,
    "id" : 428457050109382657,
    "id_str" : "428457050109382657",
    "in_reply_to_screen_name" : null,
    "in_reply_to_status_id" : null,
    "in_reply_to_status_id_str" : null,
    "in_reply_to_user_id" : null,
    "in_reply_to_user_id_str" : null,
    "lang" : "en",
    "place" : null,
    "user" : { "contributors_enabled" : false,
        "follow_request_sent" : null,
        "followers_count" : 1218,
        "id" :33333,
        "id_str" : "609465835",
        "is_translation_enabled" : false,
        "is_translator" : false,
        "lang" : "en",
        "listed_count" : 3,
        "location" : "N",
        "verified" : false
      }
    }]
    

    ,输出

    Json Output

    我正在尝试使用Newtonsoft Json反序列化,但失败了。

    以下是代码:

            dynamic dynObj = JsonConvert.DeserializeObject(twitAuthResponse);
            foreach (var data in dynObj.user.data)
            {
                //Console.WriteLine("{0}", data.name);
                foreach (var fql in data.user)
                {
                    foreach (JProperty keyValue in fql)
                    {
                        Console.WriteLine("\t{0} : {1}", keyValue.Name, keyValue.Value);
                    }
                }
            }
    

    上面的代码返回错误“'Newtonsoft.Json.Linq.JArray'不包含'user'的定义。”

    有人可以帮帮我吗?提前谢谢!

2 个答案:

答案 0 :(得分:3)

请查看以下文章:http://www.hanselman.com/blog/NuGetPackageOfTheWeek4DeserializingJSONWithJsonNET.aspx

您需要指定要反序列化的对象的类型,而不是使用动态。

类似于:var twitterJson = JsonConvert.DeserializeObject<TwitterResponse>(twitAuthResponse);

只要此.NET对象与JSON的架构匹配,它就会自动映射,您可以访问强类型的所有属性和值。

答案 1 :(得分:2)

终于解决了,

dynamic stuff = JsonConvert.DeserializeObject(twitAuthResponse);

foreach (JObject item in stuff)
{
    foreach (JProperty trend in item["user"])
    {
        if (trend.Name == "name")
        {
             // GET NAME
        }
        else if (trend.Name == "followers_count")
        {
             // GET COUNT
        }
        else if (trend.Name == "profile_image_url")
        {
             // GET PROFILE URL
        }
    }
}