C# 如何反序列化复杂的嵌套JSON?
在我的c#项目中,我想访问复杂嵌套的JSON中的特定信息(POI名称和距离) 此JSON是Azure Maps API调用的结果 我已尝试将其反序列化为对象。但是这个JSON太复杂了,我无法做到这一点 提取我需要的信息的最佳方法是什么C# 如何反序列化复杂的嵌套JSON?,c#,json,azure-maps,C#,Json,Azure Maps,在我的c#项目中,我想访问复杂嵌套的JSON中的特定信息(POI名称和距离) 此JSON是Azure Maps API调用的结果 我已尝试将其反序列化为对象。但是这个JSON太复杂了,我无法做到这一点 提取我需要的信息的最佳方法是什么 { "summary": { "query": "university", "queryType": "NON_NEAR", "queryTime": 103, "numResults": 1, "offset": 0,
{
"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
}
}
]
}
]
}
第1步:
在JSON解析器网站中解析JSON,如
这将帮助您理解内容以及如何将其翻译为对象。
例如,JSON字符串给出以下结果:
第二步:
打开本网站的查询工具,这将帮助您找到所需信息的对象路径。
例如,对于JSON字符串,要访问POI名称:
第三步:
在VisualStudio项目中,在共享库中安装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;
使用Newtonsoft.Json;
动态NewObject=JsonConvert.DeserializeObject(JSONstring);
字符串名称=NewObject.results[0].poi.Name;
字符串距离=NewObject.results[0].dist;
您至少有两种可能的解决方案:
要么创建反映所需json内容的类
public class MyJSON
{
public Summary summary { get; set; }
public List<Result> results { get; set; }
...
}
public class Summary
{
public string query { get; set; }
...
}
公共类MyJSON
{
公共摘要摘要{get;set;}
公共列表结果{get;set;}
...
}
公开课摘要
{
公共字符串查询{get;set;}
...
}
然后可以使用Newtonsoft.Json反序列化
JsonConvert.DeserializeObject<MyJSON>(jsonstring);
JsonConvert.DeserializeObject(jsonstring);
或者可以直接反序列化到动态对象,并直接按名称访问属性
dynamic data = JsonConvert.DeserializeObject<dynamic>(jsonstring);
string query = data[0].summary.query;
dynamic data=JsonConvert.DeserializeObject(jsonstring);
字符串查询=数据[0]。summary.query;
解决方案1要求您首先创建类,但访问速度更快、更安全(不太容易出现错误命名或数据结构更改)
解决方案2更加灵活多变,您只需访问所需内容。但是,如果尝试访问json对象中不存在的属性,则可能会出现异常。我通过
然后可以使用Newtonsoft对它们进行反序列化
RootObject root=JsonConvert.DeserializeObject(JSONstring)
公共类地理偏见
{
公共双lat{get;set;}
公共双lon{get;set;}
}
公开课摘要
{
公共字符串查询{get;set;}
公共字符串查询类型{get;set;}
公共int查询时间{get;set;}
public int numResults{get;set;}
公共整数偏移量{get;set;}
公共整数totalResults{get;set;}
公共int fuzzyLevel{get;set;}
公共GeoBias GeoBias{get;set;}
}
公共类名
{
公共字符串nameLocale{get;set;}
公共字符串名称{get;set;}
}
公共类分类
{
公共字符串代码{get;set;}
公共列表名称{get;set;}
}
公共类Poi
{
公共字符串名称{get;set;}
公用字符串电话{get;set;}
公共字符串url{get;set;}
公共列表类别{get;set;}
公共列表分类{get;set;}
}
公共课堂演讲
{
公共字符串streetName{get;set;}
公共字符串市政subdivision{get;set;}
公共字符串{get;set;}
公共字符串CountrySecondarySubition{get;set;}
公共字符串{get;set;}
公共字符串postalCode{get;set;}
公共字符串countryCode{get;set;}
公共字符串国家{get;set;}
公共字符串countryCodeISO3{get;set;}
公共字符串freeformAddress{get;set;}
}
公共阶级地位
{
公共双lat{get;set;}
公共双lon{get;set;}
}
公共类TopLeftPoint
{
公共双lat{get;set;}
公共双lon{get;set;}
}
公共类BtmRightPoint
{
公共双lat{get;set;}
公共双lon{get;set;}
}
公共类视口
{
公共TopLeftPoint TopLeftPoint{get;set;}
公共BtmRightPoint BtmRightPoint{get;set;}
}
公开课职位2
{
公共双lat{get;set;}
公共双lon{get;set;}
}
公共类入口点
{
公共字符串类型{get;set;}
公共位置2位置{get;set;}
}
公开课成绩
{
公共字符串类型{get;set;}
公共字符串id{get;set;}
公共双倍分数{get;set;}
公共双区{get;set;}
公共字符串信息{get;set;}
公共Poi Poi{get;set;}
公共广播地址{get;set;}
公共位置位置{get;set;}
公共视口{get;set;}
公共列表入口点{get;set;}
}
公共类根对象
{
公共摘要摘要{get;set;}
公共列表结果{get;set;}
}
大家好,欢迎来到Stack Overflow。请花点时间回顾以下操作指南:和。到目前为止,您尝试了什么?可能重复了一个开源项目,该项目为Azure Maps REST服务提供了一个.NET客户端。还有一个NuGet软件包。你可以在这里找到:Azure地图计划在今年晚些时候为rest服务提供一个官方的.NET客户端。这是一个很好的网站。谢谢分享。
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; }
}