-2

I have a big problem parsing JSON in C# in Windows Phone 8 app.

The application close when I try to execute this part of the code. The problem append when the C# code deserialize json.

See below the code : C# (deserialization + class forms) and JSON result.

Thanks for your answers.

public partial class MyGrades : PhoneApplicationPage
{
    string token = string.Empty;
    string mail = string.Empty;


    public class MyGradesJson
    {
        [JsonProperty("periods")]
        public Periods periods { get; set; }
    }

    public class Periods
    {
        [JsonProperty("period")]
        public List<Perioddata> perioddata { get; set; }
    }

    public class Perioddata
    {
        [JsonProperty("period")]
        public Period period { get; set; }
    // [JsonProperty("name")]
    // public List disciplines { get; set; }
    }

    public class Period
    {
        [JsonProperty("id")]
        public int id { get; set; }
        [JsonProperty("name")]
        public string name { get; set; }
        [JsonProperty("start_date")]
        public string start_date { get; set; }
        [JsonProperty("end_date")]
        public string end_date { get; set; }
    }

    HttpResponseMessage response = await httpClient.SendAsync(requestMessage);
    string responseAsString = await response.Content.ReadAsStringAsync();
    var resJson = JsonConvert.DeserializeObject<Periods>(responseAsString);
}

Here is the Json answer :

{
"periods":[
{
"period":{
"id":1,
"name":"Year 1",
"start_date":"2000-01-01",
"end_date":"2001-06-30"
},
"disciplines":[
{
"discipline":{
"id":6,
"name":"Potions"
},
"grades":[
{
"id":11,
"note":2,
"coefficient":2,
"assessment":"yolo",
"teacher":{
"id":2,
"user_id":4,
"login":"snape_se",
"name":"Snape, Severus"
}
},
{
"id":15,
"note":10,
"coefficient":1,
"assessment":"test",
"teacher":{
"id":2,
"user_id":4,
"login":"snape_se",
"name":"Snape, Severus"
         }
     }
  ]
     }
     ]
     }
   ]
}
WeSt
  • 2,628
  • 5
  • 22
  • 37
  • 1
    "application close": what does your global exception handler show? (I you haven't set one then do so first.) Otherwise unhandled exceptions can be picked up with a debugger. Until you have details of the exception you're wasting time. – Richard Jan 23 '15 at 11:21
  • I tried many many times to show up the debugger console but impossible to reach my goal. – JohnnyJohn Jan 23 '15 at 11:26
  • VS has various options. 1. Check the Output window for the exception, and then use Debug | Exception to break when it is thrown. 2. See http://stackoverflow.com/q/793100/67392 to set a global handler. 3. In the thread pool surround your code with a try/catch block (and `Debugger.Break()` is a helpful method). – Richard Jan 23 '15 at 11:28
  • "I tried many many times to show up the debugger console but impossible to reach my goal" Then your question should be about that problem. You need to solve that problem to move forward. – spender Jan 23 '15 at 11:30
  • Do you mind helping me in showing up the debugging console ? Because all of the information that I found in the internet did not help me a lot. Thanks ! – JohnnyJohn Jan 23 '15 at 11:43
  • I tried to debug with a simple if and a listbox : if (resJson.perioddata == null) listBox2.Items.Add("null"); else listBox2.Items.Add("ok"); and its "null". – JohnnyJohn Jan 23 '15 at 11:46
  • @JohnnyJohn Fiddler is your friend... http://www.telerik.com/fiddler – Paul Zahra Jan 23 '15 at 12:01

1 Answers1

1

I think your app is broken because your have null in your resJson. The JSON you provided will not be deserialized into Perods object instance. Try to generate C# classes from JSON. If this feature is not avaliable in your version of VS, try to use online tools. For example http://json2csharp.com/

Anton Semenov
  • 6,227
  • 5
  • 41
  • 69