简体   繁体   English

C# linq to json 如何读取数组中的属性值

[英]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.

相关问题 使用 Json.Z7FB58B61118DFE34058E05B86C 获取 JSON 数组 C# 的值 - Get the value of a JSON Array C# using Json.Linq 使用 C# 读取 json 值而不使用属性 - Read json value using C# without using Property C#如何在LINQ查询中访问SelectListItem的Selected属性的值? - C# How to access value of Selected property of SelectListItem in LINQ query? 将C#Model Date属性的数组转换为JSON Date值 - Convert an array of c# Model Date property to JSON Date value 如何使用 C# 访问嵌套在 JSON 响应中两级数组中的值? - How can I access a value nested within two levels of array in a JSON Response using C#? 如何修复错误的Json到C#将字符串数组反序列化为具有空值的属性? - How to fix wrong Json to C# deserialization of array of strings into property with null value? 如何将包含具有单个属性名称和值的对象数组的 JSON 反序列化为 c# model? - How to deserialize JSON containing an array of objects with a single property name and value each into a c# model? LINQ C#到MVC中的JSON对象数组 - LINQ C# to Array JSON Object in MVC C# 构造函数如何为只读属性赋值? - How can C# constructor assign value to read only property? 如何使用C#查询JSON数组以获取特定属性 - How to query JSON array with C#, For a specific Property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM