简体   繁体   中英

deserialize nested json data c#

I am using javascriptserializer to deserialize json data. I am stuck on how to parse this data and assign the value to a variable.

json:

  {
    "data1": {
"EntityList": "Attribute",
"KeyName": "AkeyName",
"Value": "Avalue"
    },
    "data2": {
        "Id": "jsdksjkjdiejkwp12193jdmsldm",
        "Status": "OK"
    }
}

I need to assign the values of EntityList, KeyName in data1 to a variable. I read this json string into a variable data

c#:

var data = "json string"; //variable with json string
JavaScriptSerializer jss = new JavaScriptSerializer();
dynamic drecord = jss.Deserialize<dynamic>(data);

I am trying to parse this nested json into 2 variables EntityList & KeyName.

If I understood you correct so it must be smth like this:

        JavaScriptSerializer jss = new JavaScriptSerializer();
        dynamic record = jss.Deserialize<dynamic>(data);
        var data1 = record["data1"];

        var entityList = data1["EntityList"];
        var keyName = data1["KeyName"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM