简体   繁体   中英

vb.net class structure for complex JSON strings

Here's how my JSON string looks like:

[

{
"startTime":"2014-09-22T03:00Z",
"endTime":"2014-09-22T04:00Z",
"timeElapsed":60,
"employee":{
    "PictureIDImage":{"uri":"empPhoto01.png"},
    "EmpID":"203",
    "employeeId":"65487"
    },
"employeeId":"65487",
"department":{
    "departmentID":"12333",
    "rootId":"9921466",
    "seriesId":"9921466",
    "HelpCategories":["Consumer"],
    "PictureIDImage":{"uri":"photo01.png"},
    "entityType":"Show"
    }
},

{
"startTime":"2014-09-22T03:00Z",
"endTime":"2014-09-22T04:00Z",
"timeElapsed":60,
"employee":{
    "PictureIDImage":{"uri":"empPhoto01.png"},
    "EmpID":"203",
    "employeeId":"65487"
    },
"employeeId":"65487",
"department":{
    "departmentID":"12333",
    "rootId":"9921466",
    "seriesId":"9921466",
    "HelpCategories":["Consumer"],
    "PictureIDImage":{"uri":"photo01.png"},
    "entityType":"Show"
    }
}
]

I have created an Employee and Department classes. Then I have another class called Company that with all entities as well as a variable defined as Employee and Department. (due to sensitivity of the data I have scrubbed a lot of information. In case if something is messed up in the json string, I apologize!

Now, having said that when I do the following

Dim obj As List(Of StationAiring)
obj = JsonConvert.DeserializeObject(Of List(Of StationAiring))(html)

Error: Cannot deserialize the current JSON object (eg {"name":"value"}) into type 'Listings_Matching.frmEmployeeMatching+Employee[]' because the type requires a JSON array (eg [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (eg [1,2,3]) or change the deserialized type so that it is a normal .NET type (eg not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path '[0].Employee.empID', line 1, position 100.

Any assistance to point me in the right direction to fix the error / class definition is appreciated!!

It's hard to give you an accurate answer without seeing your Company class, but looking at the error message, it looks like Employee is defined as an array or List in your Company class. I can see in your JSON that employee is not a JSON array. I suggest either (1) changing Employee in your Company class accordingly, or (2) fix your JSON so that employee is an array, like

"employee":[{
"PictureIDImage":{"uri":"empPhoto01.png"},
"EmpID":"203",
"employeeId":"65487"
}],

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