如何在VB.NET Newtonsoft中解析Json孙子

时间:2017-01-21 18:57:49

标签: json vb.net parsing descendant grandchild

我在列表框中显示21,黑杰克,21时遇到问题

我只能在列表框中显示喜欢的游戏,游戏

还有如何计算问卷中的问题数量?

Json数据

 {
   "id": 1,
   "status": "DRAFT",
   "title": "GAMES",
   "author": "foo",

   "questionnaires": [
          {
           "id": 1,
           "question": "Favorite game",
           "answers": [
                        "tongits",
                        "black jack",
                          "21"
                      ]
           },
         {
          "id": 2,
          "question": "game",
          "answers": [
                       "basketball",
                       "volleyball"
                     ]
          }
      ]
  }

这是我在VB中的代码

Dim o As JObject = JObject.Parse(json)

Dim results As List(Of JToken) = o.Children().ToList

For Each item As JProperty In results

item.CreateReader()
Select Case item.Name
    Case "questionnaires"
        Dim question As String
        For Each subitem As JObject In item.Values
            listbox1.item.add(question)
        Next
End Select
Next

很乐意感谢谁会帮助

1 个答案:

答案 0 :(得分:0)

你有一个拼写错误的“问卷”,因为你的JSON中不存在(“问卷调查”)。您也不需要搜索它。下面是您可以参考的Javascript伪代码。应该与VB.Net非常相似。

var jsonData = '{"id": 1,   "status": "DRAFT",  "title": "GAMES",   "author": "foo", "questionnaires": [          {          "id": 1,      "question": "Favorite game",           "answers": [                        "tongits",                        "black jack",                          "21"                      ]           },         {          "id": 2,          "question": "game",          "answers": [                       "basketball",                       "volleyball"                     ]          }      ]  }';    
var jsonObj = JSON.parse(jsonData);
for (var question in jsonObj.questionnaires)
{
  console.log('ID:' + jsonObj.questionnaires[question].id);
  console.log('Question:' + jsonObj.questionnaires[question].question);
  for (var answerToQuestion in jsonObj.questionnaires[question].answers)
  {
    console.log('--->Answer:' + jsonObj.questionnaires[question].answers[answerToQuestion]);
  }
}

<强>结果:

ID:1
Question:Favorite game
--->Answer:tongits
--->Answer:black jack
--->Answer:21
ID:2
Question:game
--->Answer:basketball
--->Answer:volleyball