Search code examples
c#jsongeojson

C# search in JSON array data


My first method converting input data returns JSON and its type is string. Return string looks like:

[{
    "coordinates": [102.5, 24.123],
    "type": "Point",
    "properties": {
        "tsu": "",
        "name": "china: yunnan province; vietnam: hanoi",   
        "yyyymmdd": "19700104"
    }
}, {
    "coordinates": [-73.7955, -51.943],
    "type": "Point",
    "properties": {
        "tsu": "tsu",
        "name": "s. chile",
        "yyyymmdd": "19700614"
    }
}, {
    "coordinates": [28.745, 39.18],
    "type": "Point",
    "properties": {
        "tsu": "",
        "name": "turkey: demirci, manisa",
        "yyyymmdd": "19700423"
    }
}]

How to access data stored in this kind of string?


Solution

  • Step 0: Fix the typo in your JSON string (I edited your post to do that for you).

    Step 1: Go to json2csharp, put in your JSON, and let it generate the classes for you.

    Step 2: Install Json.Net from NuGet.

    Step 3: Write var root = JsonConvert.DeserializeObject<RootObject>(myJsonString);

    Step 4: Use the object like you would any other object in C#.