简体   繁体   中英

Deserialize JSON with Json.NET in Windows 8 Store App (C#)

i'm trying to program a Windows Runtime Component in C# in Visual Studio 2012 for Windows 8. I have some issues by using Json.NET to deserialize a JSON like this:

{
"header": {
    "id": 0,
    "code": 0,
    "hits": 10
},
"body": {
    "datalist": [
        {
            "name": "",
            "city": "",
            "age": 0
        },
        {
            "name": "",
            "city": "",
            "age": 0
        },
        {
            "name": "",
            "city": "",
            "age": 0
        }
    ]
}
}

My intention is to get a top-level Dictionary out of this and to interpret every value as a string. For this example you would get a dictionary with two keys (header and body) and the matching values as strings. After this you could go down the tree. A function like this

Dictionary<string, string> jsonDict =
            JsonConvert.DeserializeObject<Dictionary<string, string>>(json);

would be nice, but this one only accept string-values. Do anybody knows how to ignore the types or get it on another way?

Furthermore to get out of the body-value "{"datalist": [ { "name": "", ....}]}" a list of dictionaries.

Thanks in advance!

If you're having a problem defining your classes, a nice feature in VS 2012 allows you to generate classes to hold your JSON/XML data using the Paste Special command under Edit . For instance, your JSON created this class:

public class Rootobject
{
    public Header header { get; set; }
    public Body body { get; set; }
}

public class Header
{
    public int id { get; set; }
    public int code { get; set; }
    public int hits { get; set; }
}

public class Body
{
    public Datalist[] datalist { get; set; }
}

public class Datalist
{
    public string name { get; set; }
    public string city { get; set; }
    public int age { get; set; }
}

...which you could then deserialize your request into the type of RootObject , eg

var obj = JsonConvert.DeserializeObject<RootObject>(json);

I would use this site and deserialize as

var myObj =JsonConvert.DeserializeObject<RootObject>(json);

public class Header
{
    public int id { get; set; }
    public int code { get; set; }
    public int hits { get; set; }
}

public class Datalist
{
    public string name { get; set; }
    public string city { get; set; }
    public int age { get; set; }
}

public class Body
{
    public List<Datalist> datalist { get; set; }
}

public class RootObject
{
    public Header header { get; set; }
    public Body body { get; set; }
}

You can also use dynamic keyword without declaring any classes

dynamic myObj =JsonConvert.DeserializeObject(json);

var age = myObj.body.datalist[1].age;

And since JObject implements IDictionary<> this is also possible

var jsonDict = JObject.Parse(json);  
var age = jsonDict["body"]["datalist"][1]["age"];

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