-1

I am trying to parse a json in following format, is there any straightforward way to do it instead of recursively parse each sub-entries?

{
"Schema": "schema",
"Entities": [{
    "Type": "sometype",
    "Properties": [{
        "Type": "name",
        "Value": ["someone"]
    }, {
        "Type": "date",
        "Value": ["sometime"]
    }, {
        "Type": "description",
        "Value": ["this is the description"]
    }
   }]
}

Is it possible to extract the information as

string name = parsedJsonObject["name"]

Thanks

JQian
  • 226
  • 2
  • 9
  • This clearly is not same question as the linked one. I am asking the way to extract json info recursively with "special format" instead of simple "key-value" pairs. Taking these two as same only supposes you guys have little knowledge to differentiate the problems, and then you should not mark duplicated arbitrarily. – JQian Sep 26 '18 at 20:28

1 Answers1

1

The quickest way you can parse string is create your custom class for this JSON and use Newtonsoft.JSON.

I would suppose this class:

class MyClassForJson
{
    public string Schema { get; set; }

    public IEnumerable<MyInnerClassForJson> Entities { get; set; }
}

class MyInnerClassForJson
{
    public string Type { get; set; }

    public IEnumerable<MyInnerInnerClassForJson> Properties { get; set; }
}

class MyInnerInnerClassForJson
{
    public string Type { get; set; }

    public IEnumerable<string> Value { get; set; }
}

Then you parse it like this:

using Newtonsoft.Json;
...
var myClassObj = JsonConvert.DeserializeObject<MyClassForJson>(jsonString);
nartalex
  • 11
  • 3