-7

I am writing a C# app that is supposed to get event data from JSON

Assuming my JSON response looks like below, how do I get the city_name, image url, date and list of owner names from the response? In JSON below, image could be null and so could be owners.

Also, how would I download the image and show it in my Image control?

{
  "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
      },
      {...}
    ]
  }
}

Thanks

pixel
  • 8,210
  • 13
  • 68
  • 121

2 Answers2

2

You can use Newtonsoft JSON this way:

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); 
    }
}
Arthur Rey
  • 2,791
  • 3
  • 17
  • 38
  • This doesn't work. `Additional information: Element „Newtonsoft.Json.Linq.JProperty” There is no definition „image”.` – BWA Dec 20 '15 at 23:29
  • @ArthurRey Thanks but it does not work. Already at getting the "id" it throws RuntimeBinder.RuntimeBinderException "'Newtonsoft.Json.Linq.JProperty' does not contain a definition for 'title'" – pixel Dec 20 '15 at 23:45
  • Fixed it - `in deserializedJson.cars.car` instead of `in deserializedJson.cars` – Arthur Rey Dec 21 '15 at 10:13
1

You can use LINQ to JSON in Newtonsoft.Json to get url from JSON.

 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;
        }
BWA
  • 5,519
  • 7
  • 33
  • 44
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Rob Dec 20 '15 at 22:26
  • @Karol Thanks Karol, but the cars above in reality is "events" and "car" is "event". As a result, if I try to do what you suggested with foreach (dynamic car in dynamicEvents.events.event), I get "event is an reserved word", so I can not do it. how to deal with that? – pixel Dec 20 '15 at 23:50
  • 1
    @dbnex14 In c# if you want use reserved keyword you must prefix it by `@`. Use `events.@event` – BWA Dec 20 '15 at 23:59
  • @Karol Thanks so much, that is it. But what if I dont have thumb url? I get "'Newtonsoft.Json.Linq.JValue' does not contain a definition for 'thumb'" error. How do I handle possibility of null values? – pixel Dec 21 '15 at 00:04
  • 1
    @dbnex14 You can use [SelectToken](http://www.newtonsoft.com/json/help/html/SelectToken.htm). **SelectToken returns the child token or a null reference if a token couldn't be found at the path's location.** Or, just handle exception and continue with next event. – BWA Dec 21 '15 at 00:31
  • @Karol Thanks, that worked. Any idea how to get list of all owners' names? Thanks a million – pixel Dec 21 '15 at 00:49
  • 1
    Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/98512/discussion-between-karol-and-dbnex14). – BWA Dec 21 '15 at 00:57