[英]C# linq to json how to read a property value within a Array
I have a JSON file in which I would like to obtain a property value land_use_type
inside of an Array property records
.我有一个 JSON 文件,我想在其中获取 Array 属性
records
的属性值land_use_type
。 my first attempt was to use the Linq to JSON with Newtonsoft reference, but the LINQ always send me this message:我的第一次尝试是使用带有 Newtonsoft 参考的 Linq to JSON,但 LINQ 总是向我发送此消息:
System.Collections.Generic.List'1[Newtonsoft.Json.Linq.JToken]
System.Collections.Generic.List'1[Newtonsoft.Json.Linq.JToken]
C# code: C#代码:
string path = @"C:\...\json1.json";
using (StreamReader read = File.OpenText(path))
{
JObject jsondata = (JObject)JToken.ReadFrom(new JsonTextReader(read));
string bearing = (string)jsondata["bearing"];
string distance = (string)jsondata["distance"];
string source = (string)jsondata["source"];
var Land_use = from x in jsondata["records"]
select x["land_use_type"];
Console.WriteLine(String.Format("{0}\n{1}\n{2}\n{3}", bearing, distance, source, Land_use.ToList()));
JSON file: JSON 文件:
{
...
"records": [
{
"crop_primary": 0,
"crop_primary_coverage": null,
"crop_secondary": 0,
"crop_secondary_coverage": null,
"crop_tertiary": 0,
"crop_tertiary_coverage": null,
"date_created": "2017-02-27T20:25:28.981681",
"date_updated": "2017-02-27T20:25:28.981681",
"history": [
{
"data": "{\"crop_primary\": 0, \"crop_primary_coverage\": null, \"crop_secondary\": 0, \"crop_secondary_coverage\": null, \"crop_tertiary\": 0, \"crop_tertiary_coverage\": null, \"date_created\": \"2017-02-27T20:25:28.981681\", \"date_updated\": \"2017-02-27T20:25:28.981681\", \"id\": 172812, \"intensity\": 0, \"land_use_type\": 3, \"location_id\": 272769, \"month\": 2, \"ndvi\": null, \"ndvi_mean\": null, \"protected\": false, \"rating\": 0, \"scale\": -1, \"source_class\": null, \"source_description\": \"mobile_application\", \"source_id\": null, \"source_type\": \"ground\", \"user_id\": 140, \"water\": 0, \"year\": 2017}",
"date_edited": "2017-02-27T20:25:29.359834",
"id": 66588,
"record_id": 172812,
"user_id": 140
}
],
"id": 172812,
"intensity": 0,
"land_use_type": 3,
"location_id": 272769,
"month": 2,
"ndvi": null,
"ndvi_mean": null,
"protected": false,
"rating": 0,
"scale": -1,
"source_class": null,
"source_description": "mobile_application",
"source_id": null,
"source_type": "ground",
"user_id": 140,
"water": 0,
"year": 2017
}
],
...
}
You can try this in your case:你可以在你的情况下试试这个:
var Land_use = jsondata["records"].Values("land_use_type").Single().ToString();
Land_use
will be "3"
. Land_use
将为"3"
。
Or you can do:或者你可以这样做:
var Land_use = var Land_use = jsondata["records"].Values("land_use_type").FirstOrDefault()?.Value<string>();
If you are expecting records
to contain more than one item its array, you may want to use this approach.如果您希望
records
在其数组中包含多个项目,您可能需要使用这种方法。
var Land_use = from x in jsondata["records"]
select x.Value<string>("land_use_type");
Console.WriteLine($"Land use: {string.Join(",", Land_use)}");
Console.ReadLine();
This will give you an output of Land use: 3,4
-- if there is more than one.这将为您提供
Land use: 3,4
的输出Land use: 3,4
-- 如果有多个。 If there is no result, then you'll simply end up with a string like Land use:
如果没有结果,那么您最终会得到一个类似
Land use:
的字符串Land use:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.