0


I have this JSON data and I need to get some specific values in C#.

{
    "info": {
        "statuscode": 0,
        "copyright": {
            "text": "© 2017 MapQuest, Inc.",
            "imageUrl": "http://api.mqcdn.com/res/mqlogo.gif",
            "imageAltText": "© 2017 MapQuest, Inc."
         },
         "messages": []
      },
      "options": {
          "maxResults": -1,
          "thumbMaps": true,
          "ignoreLatLngInput": false
      },
      "results": [
      {
            "providedLocation": {
                 "location": "mannheim, heppenheimer str"
          },
            "locations": [
                {
                    "street": "Heppenheimer Straße",
                    "adminArea6": "",
                    "adminArea6Type": "Neighborhood",
                    "adminArea5": "Mannheim",
                    "adminArea5Type": "City",
                    "adminArea4": "",
                    "adminArea4Type": "County",
                    "adminArea3": "Baden-Württemberg",
                    "adminArea3Type": "State",
                    "adminArea1": "DE",
                    "adminArea1Type": "Country",
                    "postalCode": "68309",
                    "geocodeQualityCode": "B3CAA",
                    "geocodeQuality": "STREET",
                    "dragPoint": false,
                    "sideOfStreet": "N",
                    "linkId": "u0y19uvgm4pf",
                    "unknownInput": "",
                    "type": "s",
                    "latLng": {
                          "lat": 49.509084,
                          "lng": 8.522526
                    },                     
                },
                {
                    "street": "Heppenheimer Straße",
                    "adminArea6": "",
                    "adminArea6Type": "Neighborhood",
                    "adminArea5": "Mannheim",
                    "adminArea5Type": "City",
                    "adminArea4": "",
                    "adminArea4Type": "County",
                    "adminArea3": "Baden-Württemberg",
                    "adminArea3Type": "State",
                    "adminArea1": "DE",
                    "adminArea1Type": "Country",
                    "postalCode": "68309",
                    "geocodeQualityCode": "B3CAA",
                    "geocodeQuality": "STREET",
                    "dragPoint": false,
                    "sideOfStreet": "N",
                    "linkId": "u0y19uees7fb",
                    "unknownInput": "",
                    "type": "s",
                    "latLng": {
                            "lat": 49.507762,
                            "lng": 8.519397
                    },
                 
                },
                {
                    "street": "Heppenheimer Straße",
                    "adminArea6": "",
                    "adminArea6Type": "Neighborhood",
                    "adminArea5": "Mannheim",
                    "adminArea5Type": "City",
                    "adminArea4": "",
                    "adminArea4Type": "County",
                    "adminArea3": "Baden-Württemberg",
                    "adminArea3Type": "State",
                    "adminArea1": "DE",
                    "adminArea1Type": "Country",
                    "postalCode": "68309",
                    "geocodeQualityCode": "B3CAA",
                    "geocodeQuality": "STREET",
                    "dragPoint": false,
                    "sideOfStreet": "N",
                    "linkId": "u0y1dj0msj0y",
                    "unknownInput": "",
                    "type": "s",
                    "latLng": {
                            "lat": 49.51086,
                            "lng": 8.525907
                    },
                  
                }
            ]
        }
    ]
}

I already use JSON.NET and know I have to deserialize it first. But I dont know how to get the "lat" and "lng" values. Im my case I would have to get all 3 of those and compare them.

dest2412
  • 27
  • 3
  • What is the problem with JSON.NET? You could define classes that correspond to your json structure and deserialize this json document into one complex c# object. So your lat and lng values will be available as objects properties. – NamiraJV Mar 11 '17 at 12:59
  • [easyClasses](http://json2csharp.com/) Use this site to build your classes if you have issues building on your own. – vipersassassin Mar 11 '17 at 13:14

2 Answers2

1

First create C# classes to mimic the structure for your JSON structure by following the steps in my answer here.

Then simply do this:

var data = JsonConvert.DeserializeObject<Rootobject>("YourJsonString");
var latLng = data.results[0].locations[0].latLng;
var lat = latLng.lat;
var lng = latLng.lng;
Community
  • 1
  • 1
CodingYoshi
  • 25,467
  • 4
  • 62
  • 64
1

You don't have to create a class that represents your JSON data. You can query into JSON like this:

var json = File.ReadAllText("ex.json");
var jobject = JObject.Parse(json);
var results = jobject["results"];
foreach (var result in results)
{
    var locations = result["locations"];
    foreach (var location in locations)
    {
        var pair = location["latLng"];
        Console.WriteLine("Lat: {0}, Lng: {1}", pair["lat"], pair["lng"]);
    }
}
Mert Akcakaya
  • 3,109
  • 2
  • 31
  • 42