如何反序列化复杂的嵌套JSON?

时间:2019-03-27 18:07:20

标签: c# json azure-maps

在我的c#项目中,我想访问复杂且嵌套的 JSON 中的特定信息(POI 名称距离)。 / p>

此JSON是Azure Maps API调用的结果。

我试图将其反序列化为一个对象。但是此JSON太复杂了,我无法做到。

提取所需信息的最佳方法是什么?

{
  "summary": {
    "query": "university",
    "queryType": "NON_NEAR",
    "queryTime": 103,
    "numResults": 1,
    "offset": 0,
    "totalResults": 216684,
    "fuzzyLevel": 1,
    "geoBias": {
      "lat": 48.008446,
      "lon": 7.821583
    }
  },
  "results": [
    {
      "type": "POI",
      "id": "DE/POI/p0/1505647",
      "score": 2.574,
      "dist": 774.6544330765787,
      "info": "search:ta:276009006412786-DE",
      "poi": {
        "name": "Universität Freiburg Medizinische Fakultät",
        "phone": "+(49)-(761)-27072350",
        "url": "www.med.uni-freiburg.de",
        "categories": [
          "college/university"
        ],
        "classifications": [
          {
            "code": "COLLEGE_UNIVERSITY",
            "names": [
              {
                "nameLocale": "en-US",
                "name": "college/university"
              }
            ]
          }
        ]
      },
      "address": {
        "streetName": "Elsässer Straße",
        "municipalitySubdivision": "Mooswald",
        "municipality": "Freiburg im Breisgau",
        "countrySecondarySubdivision": "Freiburg im Breisgau",
        "countrySubdivision": "Baden-Württemberg",
        "postalCode": "79110",
        "countryCode": "DE",
        "country": "Germany",
        "countryCodeISO3": "DEU",
        "freeformAddress": "Elsässer Straße, 79110 Freiburg Im Breisgau"
      },
      "position": {
        "lat": 48.00894,
        "lon": 7.83197
      },
      "viewport": {
        "topLeftPoint": {
          "lat": 48.00984,
          "lon": 7.83063
        },
        "btmRightPoint": {
          "lat": 48.00804,
          "lon": 7.83331
        }
      },
      "entryPoints": [
        {
          "type": "main",
          "position": {
            "lat": 48.00931,
            "lon": 7.83259
          }
        }
      ]
    }
  ]
}

3 个答案:

答案 0 :(得分:1)

步骤1: 在JSON解析器网站(例如https://jsonparser.org)中解析JSON 这将帮助您理解内容以及如何将其转换为对象。 例如,您的JSON字符串给出以下结果:

enter image description here

第二步: 打开该网站的查询工具,这将帮助您找到所需信息的对象路径。 例如,对于您的JSON字符串,访问POI名称:

enter image description here

第3步: 在Visual Studio项目中,在共享库中安装NuGet包:Newtonsoft.Json和Microsoft.CSharp。 如果要在单独的库中处理JSON,请同时在主项目中安装Newtonsoft.Json NuGet软件包。

第4步: 如果 JSONstring 是您的JSON字符串:

using Newtonsoft.Json;

dynamic NewObject = JsonConvert.DeserializeObject<dynamic>(JSONstring);


string Name = NewObject.results[0].poi.name;

string Distance = NewObject.results[0].dist;

答案 1 :(得分:1)

您至少有2种解决方案:

要么创建可反映您期望的json内容的类

public class MyJSON
{
  public Summary summary { get; set; }

  public List<Result> results { get; set; }
  ...
}

public class Summary
{
   public string query { get; set; }
   ...
}

然后您可以使用Newtonsoft.Json反序列化

JsonConvert.DeserializeObject<MyJSON>(jsonstring);

或者您可以直接反序列化为动态对象并直接按名称访问属性。

  dynamic data = JsonConvert.DeserializeObject<dynamic>(jsonstring);      
  string query = data[0].summary.query;

解决方案1要求您首先创建类,但访问速度更快且更安全(不易出现错误的命名或数据结构更改)

解决方案2更具可变性和灵活性,您只需访问所需的内容即可。但是,如果尝试访问json对象中不存在的属性,则可能会出现异常。

答案 2 :(得分:1)

我通过json2csharp转换了json 然后您可以使用Newtonsoft对它们反序列化

RootObject root = JsonConvert.DeserializeObject(JSONstring)

   public class GeoBias
    {
        public double lat { get; set; }
        public double lon { get; set; }
    }

    public class Summary
    {
        public string query { get; set; }
        public string queryType { get; set; }
        public int queryTime { get; set; }
        public int numResults { get; set; }
        public int offset { get; set; }
        public int totalResults { get; set; }
        public int fuzzyLevel { get; set; }
        public GeoBias geoBias { get; set; }
    }

    public class Name
    {
        public string nameLocale { get; set; }
        public string name { get; set; }
    }

    public class Classification
    {
        public string code { get; set; }
        public List<Name> names { get; set; }
    }

    public class Poi
    {
        public string name { get; set; }
        public string phone { get; set; }
        public string url { get; set; }
        public List<string> categories { get; set; }
        public List<Classification> classifications { get; set; }
    }

    public class Address
    {
        public string streetName { get; set; }
        public string municipalitySubdivision { get; set; }
        public string municipality { get; set; }
        public string countrySecondarySubdivision { get; set; }
        public string countrySubdivision { get; set; }
        public string postalCode { get; set; }
        public string countryCode { get; set; }
        public string country { get; set; }
        public string countryCodeISO3 { get; set; }
        public string freeformAddress { get; set; }
    }

    public class Position
    {
        public double lat { get; set; }
        public double lon { get; set; }
    }

    public class TopLeftPoint
    {
        public double lat { get; set; }
        public double lon { get; set; }
    }

    public class BtmRightPoint
    {
        public double lat { get; set; }
        public double lon { get; set; }
    }

    public class Viewport
    {
        public TopLeftPoint topLeftPoint { get; set; }
        public BtmRightPoint btmRightPoint { get; set; }
    }

    public class Position2
    {
        public double lat { get; set; }
        public double lon { get; set; }
    }

    public class EntryPoint
    {
        public string type { get; set; }
        public Position2 position { get; set; }
    }

    public class Result
    {
        public string type { get; set; }
        public string id { get; set; }
        public double score { get; set; }
        public double dist { get; set; }
        public string info { get; set; }
        public Poi poi { get; set; }
        public Address address { get; set; }
        public Position position { get; set; }
        public Viewport viewport { get; set; }
        public List<EntryPoint> entryPoints { get; set; }
    }

    public class RootObject
    {
        public Summary summary { get; set; }
        public List<Result> results { get; set; }
    }