【问题标题】:C# get Image Url from JSON response [duplicate]C# 从 JSON 响应中获取图像 URL [重复]
【发布时间】:2016-03-26 23:36:40
【问题描述】:

我正在编写一个应该从 JSON 获取事件数据的 C# 应用程序

假设我的 JSON 响应如下所示,我如何从响应中获取 city_nameimage urldatelist of owner names?在下面的 JSON 中,image 可以为 null,所有者也可以为 null。

另外,我将如何下载图像并将其显示在我的图像控件中?

{
  "total_items": "24",
  "page_number": "1",
  "page_size": "10",
  "page_count": "3",
  "events": {
    "event": [
      {
        "url": "<event-1-url>",
        "id": "event-1",
        "city_name": "Seattle",
        "description": "car show event",
        "image": { <-- THIS COULD BE NULL, HOW TO HANDLE NULL VALUE?
          "thumb": {
            "width": "32",
            "url": "<image_url>/carshow.jpg",
            "height": "32"
          }
        },
        "date": "2015-12-09 13:20:20",
        "owners": { <-- THIS COULD BE NULL OR MULTIPLE OWNERS, HOW TO GET ALL OWNERS NAMES?
          "owner": [
            {
              "name": "John Doe",
              "id": "O12",
              "bio": "fast track racer"
            },
            {
              "name": "Tom Tomasson",
              "id": "O513",
              "bio": "fines collector"
            }
          ]
        },
      },
      {
        "url": "<event-2-url>",
        "id": "event-2",
        "city_name": "Blaine",
        "description": "toyota event",
        "image": null, <-- NO IMAGE IS PROVIDED
        "date": "2015-12-09 13:20:20",
        "owners": null, <-- NO OWNER IS PROVIDED
      },
      {...}
    ]
  }
}

谢谢

【问题讨论】:

  • 您必须添加有关您当前使用的技术堆栈的更多详细信息。

标签: c# json image


【解决方案1】:

你可以这样使用Newtonsoft JSON

dynamic deserializedJson = JsonConvert.DeserializeObject(yourJson);
foreach(dynamic car in deserializedJson.cars.car)
{
    string url = car.image.thumb.url;
    string img = url.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries)[1];

    using (WebClient client = new WebClient()) 
    {
        client.DownloadFile(url, img); 
    }
}

【讨论】:

  • 这不起作用。 Additional information: Element „Newtonsoft.Json.Linq.JProperty” There is no definition „image”.
  • @ArthurRey 谢谢,但它不起作用。已经在获取“id”时抛出 RuntimeBinder.RuntimeBinderException “'Newtonsoft.Json.Linq.JProperty' does not contain a definition for 'title'”
  • 修复它 - in deserializedJson.cars.car 而不是 in deserializedJson.cars
【解决方案2】:

您可以使用LINQ to JSON in Newtonsoft.Json 从 JSON 中获取 url。

 string stringJson = @"{
            ""total_items"": ""24"",
            ""page_number"": ""1"",
            ""page_size"": ""10"",
            ""page_count"": ""3"",
            ""cars"": {
            ""car"": [
              {
                ""url"": ""<honda-1-url>"",
                ""id"": ""honda-1"",
                ""city_name"": ""Seattle"",
                ""description"": ""black Honda"",
                ""image"": {
                    ""thumb"": {
                        ""width"": ""32"",
                        ""url"": ""<image_url>/honda1.jpg"", 
                        ""height"": ""32""
                                }
                            },
                ""date"": ""2015-12-09 13:20:20""
               },
               {
                    ""url"": ""<honda-2-url>"",
                    ""id"": ""honda-2"",
                    ""city_name"": ""Blaine"",
                    ""description"": ""red Honda"",
                    ""image"": {
                        ""thumb"": {
                        ""width"": ""32"",
                        ""url"": ""<image_url>/honda2.jpg"",
                        ""height"": ""32""
                                    }
                                },
                    ""date"": ""2015-12-09 13:20:20""
                }
                        ]
        }}";

        dynamic dynamicCars = JsonConvert.DeserializeObject(stringJson);

        foreach (dynamic car in dynamicCars.cars.car)
        {
            string url = car.image.thumb.url;
        }

【讨论】:

  • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效。
  • @Karol 谢谢卡罗尔,但实际上上面的汽车是“事件”,“汽车”是“事件”。结果,如果我尝试使用 foreach(dynamicEvents.events.event 中的动态汽车)执行您建议的操作,我会得到“事件是保留字”,所以我不能这样做。如何处理?
  • @dbnex14 在 c# 中,如果你想使用保留关键字,你必须在它前面加上 @。使用events.@event
  • @dbnex14 你可以使用SelectToken如果在路径的位置找不到令牌,则 SelectToken 返回子令牌或空引用。 或者,只处理异常并继续下一个事件。
猜你喜欢
  • 1970-01-01
  • 2021-09-15
  • 1970-01-01
  • 2017-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-01
  • 2013-12-04
相关资源
最近更新 更多