1

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.

1 Answer 1

1

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).

Sign up to request clarification or add additional context in comments.

7 Comments

Could you indicate how I would retrieve the values from the quizObj ;
@dotnetter It will be a strongly typed QuizObj. So you could, for example: string corr = quizObj.corr;
What Bennor said didn't work. @Simon It's gonna be a strongly typed QuizObj but if use the code you typed which corr property I'll get ;
yeap, that worked for me too. Could someone explain to me why we used <List<QuizObj>
Because you have a collection of items. We could also have deserialized into an array or even IEnumerable<QuizObj>.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.