gpt4 book ai didi

c# - 从 Json 文件中获取选定的结果

转载 作者:太空宇宙 更新时间:2023-11-03 15:50:42 25 4
gpt4 key购买 nike

这是我要使用的 Json 文件的示例:

{
"type": "FeatureCollection",
"totalFeatures": 213,
"features": [
{
"type": "Feature",
"id": "world_contries.1",
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[
[
[
65.53080749511719,
37.248600006103516
],
[
65.6272964477539,
37.33319854736328
]
]
]
]
},
"geometry_name": "geom",
"properties": {
"name": "Afghanistan",
"iso_3_code": "AFG",
"iso_2_code": "AF",
"area": 65209,
"name_1": "Afghanistan",
"gmi_cntry": "AFG",
"region": "Asia",
"pop2005": 25067407,
"name_12": "Afghanistan"
}
},
{
"type": "Feature",
"id": "world_contries.2",
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[
[
[
19.282489776611328,
42.18553924560547
],
[
19.397319793701172,
42.31707000732422
]
]
]
]
},
"geometry_name": "geom",
"properties": {
"name": "Albania",
"iso_3_code": "ALB",
"iso_2_code": "AL",
"area": 2740,
"name_1": "Albania",
"gmi_cntry": "ALB",
"region": "Europe",
"pop2005": 3153731,
"name_12": "Albania"
}
},
]
}

在这种类型的文件中,我想要所有要素的几何类型和坐标。

我目前正在使用这种方法访问文件:

public static List<string> getCoords(string path)
{
//List<string> layers = new List<string>();
string url = path;
WebRequest request = WebRequest.Create(url);
request.ContentType = "application/json";
request.Method = "GET";

try
{
WebResponse response = request.GetResponse();
StreamReader responseStream = new StreamReader(response.GetResponseStream());
string responseText = responseStream.ReadToEnd();
JObject o = JObject.Parse(responseText);
dynamic array = JsonConvert.DeserializeObject(responseText);
string type = array["features"].Children()["geometry"]["type"];
response.Close();
}
catch (WebException)
{
;
}
return null;
}

但它不起作用。例如:

array["features"].Children()["geometry"]["type"]

这是一个 Newtonsoft.JSon.Linq.JEnumerable

当我在我的 Visual Studio 中调试时,在结果 View 中我可以读取“MultiPolygon”,但我是否提取值?

最佳答案

我使用 json2csharp ( http://json2csharp.com ) 生成了一些与您提供的 JSON 相匹配的 C# 类。

public class Geometry
{
public string type { get; set; }
public List<List<List<List<double>>>> coordinates { get; set; }
}

public class Properties
{
public string name { get; set; }
public string iso_3_code { get; set; }
public string iso_2_code { get; set; }
public int area { get; set; }
public string name_1 { get; set; }
public string gmi_cntry { get; set; }
public string region { get; set; }
public int pop2005 { get; set; }
public string name_12 { get; set; }
}

public class Feature
{
public string type { get; set; }
public string id { get; set; }
public Geometry geometry { get; set; }
public string geometry_name { get; set; }
public Properties properties { get; set; }
}

public class RootObject
{
public string type { get; set; }
public int totalFeatures { get; set; }
public List<Feature> features { get; set; }
}

然后您可以修改您的代码,以便将其反序列化为 RootObject,这将比使用动态类型更容易处理...

var myObject = JsonConvert.DeserializeObject<RootObject>(responseText);

然后你可以像这样访问它......

foreach (var feature in myObject.features)
{
var geometryType = feature.geometry.type;
....
}

关于c# - 从 Json 文件中获取选定的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25973374/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com