Jacob C.
Jacob C.

Reputation: 117

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. my first attempt was to use the Linq to JSON with Newtonsoft reference, but the LINQ always send me this message:

System.Collections.Generic.List'1[Newtonsoft.Json.Linq.JToken]

C# code:

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:

{
  ...
  "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
}
 ],
 ...

}

Upvotes: 2

Views: 2341

Answers (2)

kat1330
kat1330

Reputation: 5332

You can try this in your case:

var Land_use = jsondata["records"].Values("land_use_type").Single().ToString();

Land_use will be "3".

Or you can do:

var Land_use = var Land_use = jsondata["records"].Values("land_use_type").FirstOrDefault()?.Value<string>();

Upvotes: 1

kghantous
kghantous

Reputation: 149

If you are expecting records to contain more than one item its array, you may want to use this approach.

    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. If there is no result, then you'll simply end up with a string like Land use:

Upvotes: 0

Related Questions