duanpuchen3142
2016-04-28 04:54
浏览 904

如何在VB.net中获取JSON数组或对象的值?

I have JSON file having following data. I just want to get the data of "naming" and "unit". Please assist me how to do this in VB.net?

[
        {
            "customerId": "999",
            "deviceId": "XXX999",
            "searchDeviceId": "D_XXX999",
            "utc": "2016-04-28T03:37:00.000Z",
            "lat": 22.5691,
            "lng": 120.3058,
            "sensors": [
                {
                    "naming": "ABC123",
                    "factor": null,
                    "unit": "k",
                    "period": null
                },
                {
                    "naming": "XYZ123",
                    "factor": null,
                    "unit": "c",
                    "period": null
                },
                .
                .
                .
                .
                .
            ]
        }
    ]

图片转代码服务由CSDN问答提供 功能建议

我的JSON文件包含以下数据。 我只想获得“命名”和“单位”的数据。 请帮助我在VB.net?

 [
 {
“customerId”:“999”,
“deviceId”:“XXX999”中执行此操作 
“searchDeviceId”:“D_XXX999”,
“utc”:“2016-04-28T03:37:00.000Z”,
“lat”:22.5691,
“lng”:120.3058,
“”传感器“  :[
 {
“命名”:“ABC123”,
“因子”:null,
“单位”:“k”,
“句号”:null 
},
 {
“ 命名“:”XYZ123“,
”因子“:null,
”单位“:”c“,
”句号“:空
},
。
。
。
。
  。
] 
} 
] 
   
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duankousong9637 2016-04-28 05:21
    已采纳

    For C# :

    JObject jResults = JObject.Parse("JsonString");
    String naming = jResults["sensors"]["naming "];
    String unit = jResults["sensors"]["unit "];
    

    For VB:

    Dim jResults As JObject = JObject.Parse("JsonString")
    Dim naming As [String] = jResults("sensors")("naming ")
    Dim unit As [String] = jResults("sensors")("unit ")
    

    You can achieve like this.

    打赏 评论
  • dongwei6700 2018-04-10 16:20

    Just in case people are looking to loop through multiple JSON Array or Object in vb.net using Newtonsoft.Json.Linq

     request = url
     request.Headers.Add("Authorization", "Bearer " + accessToken)
    
                'Get response
                response = DirectCast(request.GetResponse(), HttpWebResponse)
    
                ' Get the response stream into a reader  
                reader = New StreamReader(response.GetResponseStream())
                Dim JSONresponseFromServer As String = reader.ReadToEnd()
    
                ' Parse the content into a json object
                Dim json As String = JSONresponseFromServer
                Dim ser As JObject = JObject.Parse(json)
                Dim data As List(Of JToken) = ser.Children().ToList
    
                For Each item As JProperty In data
                    item.CreateReader()
                    Select Case item.Name
                 Case "sensors" 'each record is inside the entries array
                         For Each Entry As JObject In item.Values
                           Dim naming As String = Entry("naming ").ToList.Item(0)
                           Dim factor As String = Entry("factor").ToList.Item(0)
    ' you can continue listing the array items untill you reach the end of you array
    
                 Next
            End Select
     Next 
    

    If the Json object item is not in an array and you just want the items it returns

       For Each item As JProperty In data
           item.CreateReader()
                 Dim customerId As String = ser("customerId")
                 Dim deviceIdAs String = ser("deviceId")
       next
    

    I was getting an error using the 'Chetan Sanghani' answer with the brackets around string ex [String]

    打赏 评论

相关推荐 更多相似问题