简体   繁体   中英

C# Json Deserialization

I have been asked to work with json data in order to create a quiz game in windows phone. I knew that I had to use json.net to achive this which I have previously used in the past but the method I used in the past is no useful here.

My question is this. I have this json string

 [{"corr":"1","q":"text.","type":"0"},
    {"corr":"0","q":"text.","type":"0"},
    {"corr":"1","q":"text.","type":"0"},
    {"corr":"0","q":"text.","type":"0"},
    {"corr":"0","q":"text.","type":"0"},
    {"corr":"1","q":"text.","type":"0"},
    {"corr":"4","q":"text","a":["text","text","text","text"],"type":"1"},
    {"corr":"2","q":"text","a":["text","text","text","text"],"type":"1"},
    {"corr":"1","q":"text","a":["text","text","text","text"],"type":"1"},
    {"corr":"2","q":"text","a":["22,2%","45%","54%","67%"],"type":"1"}]

and as you can image I want to fill some List with the properties above. I have created the following class in order to represent the json objects

public class QuizObj
{
    public string corr { get; set; }
    public string q { get; set; }
    public string type { get; set; }
    public List<string> a { get; set; }
}

but I don't really know how to use it and can't find something really relevant.

Something like this should do the trick:

var quizObjs = JsonConvert.DeserializeObject<List<QuizObj>>(serializedStringValue);
string corr = quizObjs.First().corr;
// or
foreach(var quizObj in quizObjs)
{
    string corr = quizObj.corr;
    // etc
}

You will need to add a reference to NewtonSoft.Json, which you can get via NuGet (if you haven't already).

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