2

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

}
0

2 Answers 2

1

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>();
Sign up to request clarification or add additional context in comments.

3 Comments

No problem :) Please note that you have to handle result appropriately because .Single() throws an exception if the sequence is empty or if there are many!
and is there any other ways to do it beside the single()
I updated my answer. Please note that you have to take care of records and land_use_type because you are query collection. It means that you can have many records or land_use_type. I don't know what is your case, but anyway, keep in mind that you have to have code which can handles null or many.
0

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:

2 Comments

Is there any way to check if the element is null
@JacobC. are you wanting to check of x is null or Land_use? To check if x is null, you'd check by adding a where clause to the linq query and doing `where x.Value<string>("land_use_type") != null

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.