如何使用vbnet获取Json List项目

时间:2013-12-29 13:34:13

标签: json vb.net

您好我正在尝试从Json字符串中获取所有项目的列表 请温柔我是一个完全编程虚拟大声笑 我一直在搜索,直到我脸上都是蓝色的,但在How to Parse Json children in VB.NET Newtonsoft找到了最好的例子但是在尝试了所有的后,我只能从“结果”中得到第一批项目 但我需要所有来自“nilfromhere”的物品 这是Json字符串看起来像

{ “jsonrpc”: “2.0”, “结果”:[{ “RESULT1”: “noproblem1”, “结果2”: “noproblem2”, “nilfromhere”:[{ “thisone”:000, “andthis”: “nodice”,“needthis”:0.0,“andthis”:1}]}],“maybethis”:1}

这就是我所拥有的(我已经尝试过切换它们但仍然只获得前面的结果)

 Dim strJson As String = txtTester.Text
    Dim obj As JObject = JObject.Parse(strJson)
    Dim results As List(Of JToken) = obj.Children().ToList
    Dim strStart As String = ""
    Dim strOutput1 As String = ""
    Dim strOutput2 As String = ""
    For Each item As JProperty In results
        item.CreateReader()
        Select Case item.Name
            Case "jsonrpc"
                Dim strVersion As String = ""
                strVersion = item.Value.ToString
                strStart = strVersion  'Returns 2.0 so no need to be a double etc
            Case "result"
                Dim Result1 As String = ""
                Dim Result2 As String = ""
                'Dim NilFromHere as String >>> Cant convert an Array to string error when tried up here
                '~~> All these work fine below up to the nilfromhere string
                For Each resultItem As JObject In item.Values
                    Result1 = resultItem("result1") 'Returns "noproblem1"
                    Result2 = resultItem("result2") 'Returns "noproblem2"
                    ' Dim ResultTry As Object = resultItem("nilfromhere").ToString '= Error as expected!
                    ' Dim ResultTry2 As Object = resultItem("thisone").ToString '= Error as expected!
                Next
                strOutput1 += Result1 + vbTab + Result2 + vbCrLf
                'Below returns nothing but no errors! I need all the items from "nilfromhere" 
            Case "nilfromhere" ' Tried this here as it looks identical to the first one
                Dim Thisone As Object = Nothing 'Tried object because its an integer but 2.0 works ok above
                Dim AndThis As String = ""
                'Get nothing here
                For Each Iwish As JObject In item.Values
                    Thisone = Iwish("thisone").ToString 'Number values but dont think thats the problem?
                    AndThis = Iwish("andthis").ToString
                    'Other Items etc
                Next
                strOutput2 += Thisone + vbTab + AndThis + vbCrLf
                '*** Try as an array but returns same as above - Nothing but no errors!
                Dim IwishArray As JArray = item.Value    'Get the top-level array?
                For Each IwishArrayItem As JObject In IwishArray.Values
                    Thisone = IwishArrayItem("thisone")
                    AndThis = IwishArrayItem("thisone")
                Next
                strOutput2 += Thisone + vbTab + AndThis + vbCrLf
        End Select
    Next
    MsgBox(strStart)
    MsgBox(strOutput1)
    MsgBox(strOutput2)

如果有人能帮助我,我真的很感激 非常感谢提前 戴夫

1 个答案:

答案 0 :(得分:3)

不要手动解析json,而应该让Json库完成所有工作。

您可以通过定义与您尝试解析的Json匹配的类结构,然后要求json库将json字符串反序列化为该类结构来实现。它将处理所有必要的工作,例如类型转换和实例化数组或对象列表,而无需您进行任何额外的工作。

这是一个基于你的json字符串的工作示例(我必须更改第二个,然后将此更改为andthis1以使其正常工作):

顶级课程:

public class TopLevel
{
    public string jsonrpc { get; set; }
    public List<SecondLevel> result { get; set; }
}

第二级课程(包含在上面的数组结果中):

public class SecondLevel
{
    public string result1 { get; set; }
    public string result2 { get; set; }
    public List<ThirdLevel> nilfromhere { get; set; }
    public int maybethis { get; set; }
}

第三级类(存储在nillfromhere中的对象数组):

public class ThirdLevel
{
    public int thisone { get; set; }
    public string andthis { get; set; }
    public decimal needthis { get; set; }
    public int andthis1 { get; set; }
}

最后,反序列化代码:

const string json = @"{""jsonrpc"":""2.0"",""result"":[{""result1"":""noproblem1"",""result2"":""noproblem2"",""nilfromhere"":[{""thisone"":000,""andthis"":""nodice"",""needthis"":0.0,""andthis1"":1}]}],""maybethis"":1}";

var jsonObject = Newtonsoft.Json.JsonConvert.DeserializeObject<TopLevel>(json);

Debug.WriteLine(jsonObject.result[0].nilfromhere[0].andthis);