Problem parsing JSON

Hi,

I am having trouble parsing some JSON received, everything I have tried so far has failed.

I have JSON like this:

{
“templateList”:[
{
“templateKey”:380,
“templateName”:“TestHTMLForm”,
“templateType”:“HTML”
},
{
“templateKey”:383,
“templateName”:“TestHTML2”,
“templateType”:“HTML”
},
{
“templateKey”:407,
“templateName”:“TestPDF”,
“templateType”:“PDF”
}
],
“listElementCount”:3,
“resultCode”:0,
“resultMessage”:""
}

In my app I have created a class for the templates with properties matching the values in the JSON. I wanted to create an array of the templates that I could then display. But I haven’t been able to parse the JSON array. The variable I create for the JSON item that is the templateList, which is an array of JSON objects, always disappears (in the debugger) when I try to work with the template list item.

I create the JSON object with: ResultJSON = new JSONItem(jsonString) <-- this works, I see my JSON in the debugger

If ResultJSON.HasName("templateList") Then
  Dim JMTemplates As JSONItem = ResultJSON.Value("templateList")   <-- this fails, variable "JMTemplates" just disappears in the debugger
End if

I tried creating an array of JSONItem, but didn’t get it right to assign the array. I thought I need to create a JSON item that is an array, then step through each item in the array to extract each JSON item value.

The example JSON app hasn’t helped me and other questions and answers I have found haven’t help yet. Any ideas on how I can parse this?

Thanks,
Peter

The reason it disappears is that you’re declaring the variable inside the if statement.

templateList is also an array of json items. I am not sure if you want the array of items, or individual item?

Make your code look like this:

Dim JMTemplates As JSONItem If ResultJSON.HasName("templateList") Then JMTemplates = ResultJSON.Value("templateList") End if

Then you can use a call like this:

If JMTemplates<>nil and JMTemplates.IsArray then dim c as integer = JMTemplates.Count For i As Integer = 0 to c-1 Dim Template as JSONItem = JMTemplates.Child(i) // work on the individual templates here... Next i End If