JesseBuesking
JesseBuesking

Reputation: 6586

JavaScriptSerializer on JSON containing an array

I have a string of JSON like this:

{
    "letterstats":[
        {"time_taken":636,"mistake_letters":"","the_letter":"L","success":true},
        {"time_taken":216,"mistake_letters":"","the_letter":"U","success":true},
        {"time_taken":103,"mistake_letters":"","the_letter":"I","success":true}
    ],
    "word":"TEST"
}

I'm trying to use the JavaScriptSerializer to parse this, but I'm having an issue. Here's the c# code I'm using to attempt to parse this:

public class wordStats
{
    public string word { get; set; }
    List<letterStats> letterstats { get; set; }
    public wordStats() { letterstats = new List<letterStats>(); }
}

public class letterStats
{
    public int time_taken { get; set; }
    public string mistake_letters { get; set; }
    public string the_letter { get; set; }
    public bool success { get; set; }
}

JavaScriptSerializer ser = new JavaScriptSerializer();
wordStats ws = ser.Deserialize<wordStats>(jsonObj);

It's parsing out the word fine ("TEST") but is not parsing the array. I'm not sure whats going on, and I'm referencing this in order to get it to work. Does anyone have any idea of what's going wrong? Thanks

Upvotes: 2

Views: 6350

Answers (1)

m3kh
m3kh

Reputation: 7941

The letterstats property is private.

public class wordStats
{
    public string word { get; set; }
    public List<letterStats> letterstats { get; set; }
}

Upvotes: 3

Related Questions