Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从twitter解析包含多个细节的Json数据_C#_Json_Twitter_Json.net - Fatal编程技术网

C# 从twitter解析包含多个细节的Json数据

C# 从twitter解析包含多个细节的Json数据,c#,json,twitter,json.net,C#,Json,Twitter,Json.net,我从twitter获得以下输出,我只想从输出中捕获以下细节 屏幕名称 追随者人数 配置文件\u图像\u url 问题是数据在嵌套的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" : 428

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

  • 屏幕名称
  • 追随者人数
  • 配置文件\u图像\u url
  • 问题是数据在嵌套的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
      }
    }]
    
    输出是

    我正在尝试使用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'的定义”


    有人能帮我吗?提前谢谢

    请看下面的文章:

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

    类似于:
    var twitterJson=JsonConvert.DeserializeObject(twitAuthResponse)

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

    最终解决

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

    我已经试过了,但我不明白如何在嵌套结构中获取数据。我试图获取的“用户”详细信息只不过是一个嵌套的Json。这些信息拯救了我的大脑。其他方法仅适用于标准JSON结构,但这确实是可伸缩的。我将其用于web脚本API。API并不是以标准格式返回所有信息,它的工作方式很有魅力。您还可以将此动态类型用作多维数组,如stuff[0][“user”][“lang”],这正是我所要的。非常感谢你。