Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.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/3/sockets/2.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
如何获取JSON对象-C#_C#_.net_Json - Fatal编程技术网

如何获取JSON对象-C#

如何获取JSON对象-C#,c#,.net,json,C#,.net,Json,我尝试了这么多东西,但我无法取出对象下的部分 API输出 "objects": [ { "date": "Mon, 11 Sep 2017 00:00:00 GMT", "images": [ { "naturalHeight": 298, "width": 810, "url": "https://www1.google.com/files/2017/09/Feature-Image-810x298_c.png",

我尝试了这么多东西,但我无法取出对象下的部分

API输出

"objects": [
  {
    "date": "Mon, 11 Sep 2017 00:00:00 GMT",
    "images": [
      {
        "naturalHeight": 298,
        "width": 810,
        "url": "https://www1.google.com/files/2017/09/Feature-Image-810x298_c.png",
        "naturalWidth": 810,
        "primary": true,
        "height": 298
      },
      {
        "naturalHeight": 393,
        "width": 300,                    
        "title": "8 Ways to Enter",
        "url": "https://www1.google.com/files/2017/09/Feature-Image-810x298_csss.png",
        "naturalWidth": 563,
        "height": 223
      },
      {
        "naturalHeight": 300,
        "width": 169,                    
        "url": "https://www1.google.com/files/2017/09/Feature-Image-810x298_themec.png",
        "naturalWidth": 169,
        "height": 297
      },
      {
        "naturalHeight": 300,
        "width": 169,                  
        "title": "Before: Android Oreo’s Stock, Light UI",
        "url": "https://www1.google.com/files/2017/09/Feature-Image-810x298_dec.png",
        "naturalWidth": 169,
        "height": 297
      }, ...... continue
我需要获取第一个图像,其中
PRIMARY=TRUE

我尝试了以下方法,但没有成功

var dirs = JObject.Parse(json)
            .Descendants()
            .Where(x=>x is JObject)
            .Where(x=>x["primary"]!=null && x["url"]!=null)
            .Select(x =>new { URL= (string)x["primary"], PRIMARY = (string)x["url"] })
            .ToList();

var id = dirs.Find(x => x.Primary == "true").URL;

你的代码看起来像

var jObj = JObject.Parse(json)["objects"]
            .SelectMany(x => x["images"])
            .Where(x => x["primary"] != null)
            .FirstOrDefault(x => (bool)x["primary"]);
您可以更进一步,通过声明一个类

public class Image
{
    public int naturalHeight { get; set; }
    public int width { get; set; }
    public string url { get; set; }
    public int naturalWidth { get; set; }
    public bool primary { get; set; }
    public int height { get; set; }
    public string title { get; set; }
}
你可以写

var image = jObj.ToObject<Image>();
var image=jObj.ToObject();

您可以使用Newtonsoft.Json NuGet包,也可以使用其

基于Json显示的示例

    public class SampleJson
    {
        [JsonProperty("date")]
        public string Date { get; set; }
        [JsonProperty("images")]
        public List<Image> Images { get; set; }
        // nested type, you can write it out of SampleJson object brackets
        class Image
        {
             [JsonProperty("naturalHeight")]
             public int NaturalHeight { get; set; }
             [JsonProperty("width")]
             public int Width { get; set; }
             [JsonProperty("url")]
             public string Url { get; set; }
             [JsonProperty("naturalWidth")]
             public int NaturalWidth { get; set; }
             [JsonProperty("primary")]
             public bool Primary { get; set; }
             [JsonProperty("height")]
             public int Height { get; set; }
             [JsonProperty("title")]
             public string Title { get; set; }
        }
    }
public类SampleJson
{
[JsonProperty(“日期”)]
公共字符串日期{get;set;}
[JsonProperty(“图像”)]
公共列表图像{get;set;}
//嵌套类型,您可以使用SampleJson对象括号将其写入
阶级形象
{
[JsonProperty(“自然高度”)]
public int NaturalHeight{get;set;}
[JsonProperty(“宽度”)]
公共整数宽度{get;set;}
[JsonProperty(“url”)]
公共字符串Url{get;set;}
[JsonProperty(“自然宽度”)]
公共int自然宽度{get;set;}
[JsonProperty(“主”)]
公共布尔主{get;set;}
[JsonProperty(“高度”)]
公共整数高度{get;set;}
[JsonProperty(“所有权”)]
公共字符串标题{get;set;}
}
}
以及这种方法的用法

var json = HttpClinet.GetAsync("URL").Result; // your json

SampleJson obj = JsonConvert.DeserializeObject<SampleJson>(json);
var query = obj.Images.FirstOrDefault(); // Use LINQ to C# objects
var json=HttpClinet.GetAsync(“URL”).Result;//你的json
SampleJson obj=JsonConvert.DeserializeObject(json);
var query=obj.Images.FirstOrDefault();//将LINQ用于C#对象

希望有帮助。

使用Newtonsoft.json将json反序列化到运行时对象。然后您可以访问这些属性