JSON.NET反序列化 - 无法弄清楚类?

时间:2012-03-02 18:28:49

标签: .net json json.net

这是JSON:

{
  "status": "live",
  "responses": 10,
  "questions": {
    "ZuGnqOpd3w": {
       "title": "Sample Title",
       "type": "threed-grid",
       "id": "ZuGnqOpd3w",
       "label": "Sample Label."
       }
     } 
 }

和班级;

Public Class Survey
    Public Property status As String

    Public Property responses As Integer

    Public Property questions() As jQuestion()

End Class

Public Class jQuestion
    Public Property Title as String 
    ......
End Class

当我尝试反序列化时,我收到以下错误: 无法将JSON对象反序列化为类型'jQuestion []'。

有人可以帮我正确构建我的课程吗?

1 个答案:

答案 0 :(得分:0)

虽然我对json.net不太熟悉,但看起来json对象的格式不正确,这取决于你的类。由于Questions是一个数组,json中的值应该用方括号[]

包围
{
  "status": "live",
  "responses": 10,
  "questions": [
       {
        "title": "Sample Title",
        "type": "threed-grid",
        "id": "ZuGnqOpd3w",
        "label": "Sample Label."
       },
       {
        "title": "Sample Title 2",
        "type": "threed-grid 2",
        "id": "ZuGnqOpd3w 2",
        "label": "Sample Label. 2"
       }
     ] 
 }

将来验证您尝试反序列化的json格式时,您可以通过在.net中创建对象来作弊,然后查看使用json.net序列化它们时的样子。