如何在 C# 中访问此 JSON 值

How can I access this JSON values in C#


我有这个 JSON 数据,我需要在 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
                    },
                  
                }
            ]
        }
    ]
}

我已经在使用 JSON.NET 并且知道我必须先反序列化它。但我不知道如何获得 "lat" 和 "lng" 值。我的情况是我必须得到所有这三个并比较它们。

首先按照我的回答 .

中的步骤创建 C# 类 以模仿您的 JSON 结构的结构

然后只需这样做:

var data = JsonConvert.DeserializeObject<Rootobject>("YourJsonString");
var latLng = data.results[0].locations[0].latLng;
var lat = latLng.lat;
var lng = latLng.lng;

您不必创建代表您的 JSON 数据的 class。你可以 query into JSON 这样:

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"]);
    }
}