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

我的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“,
”句号“:空
},





]
}
]
</ code> </ pre>
</ div>

展开原文

原文

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
                },
                .
                .
                .
                .
                .
            ]
        }
    ]

2个回答



对于C#:</ p>

  JObject jResults = JObject.Parse(“JsonString”)  ; 
String naming = jResults [“sensors”] [“命名”];
String unit = jResults [“sensors”] [“unit”];
</ code> </ pre>

对于VB:</ p>

  Dim jResults As JObject = JObject.Parse(“JsonString”)
Dim命名为[String] = jResults(“sensors”)(“命名”)\ nDim unit As [String] = jResults(“sensors”)(“unit”)
</ code> </ pre>

你可以这样做。</ p>
</ div >

展开原文

原文

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.

drpjdfj618393
drpjdfj618393 嗨Chetan Sanghani,是否有可能只得到像customerId,deviceId,searchDeviceId .........?
大约 4 年之前 回复
dongqiang1226
dongqiang1226 我用的是我发布的。
4 年多之前 回复
dtt78245
dtt78245 非常感谢你,但有同样的错误。 我已经安装了JSON.NET。 如果您不介意,请给我示例代码?
4 年多之前 回复
dqspy04266
dqspy04266 请尝试以上代码
4 年多之前 回复
duandu8202
duandu8202 JArray jResults = JArray.Parse(“JsonString”); var tem = jResults [0] [“sensors”] [“命名”] .ToString();
4 年多之前 回复
doudi1750
doudi1750 你添加了newtonsoft.dll吗?
4 年多之前 回复
douhuan1905
douhuan1905 感谢您的回答,但我收到此错误[Newtonsoft.Json.JsonTeaderException:从JsonReader读取JObject时出错。 当前的JsonReader项不是对象:StartArray。 路径'',第1行,第1位。 我更改为JArray但仍然收到错误[System.ArgumentException:使用无效键值访问JArray值:“sensors”.Int32数组索引已被删除。]
4 年多之前 回复



以防万一人们想要使用Newtonsoft.Json.Linq在vb.net中循环遍历多个JSON数组或对象</ p>

  request = url 
request.Headers.Add(“Authorization”,“Bearer”+ accessToken)

'获取响应
response = DirectCast(request.GetResponse(), HttpWebResponse)

'将响应流送入阅读器
reader = New StreamReader(response.GetResponseStream())
Dim JSONresponseFromServer As String = reader.ReadToEnd()

'将内容解析为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()
选择Case item.Name
Case“sensors”'每条记录都在条目数组内
对于每个条目作为JObject在item.Values
Dim命名为String = Entry(“命名”)。ToList.Item(0)
Dim factor As String = Entry(“factor”)。ToList.Item(0)\ n'你可以继续列出数组项,直到你到达数组末尾

下一步
结束选择
下一步
</ code> </ pre>

如果Json对象 item不在数组中,你只想要它返回的项目</ p>

  For Each item As JProperty In data 
item.CreateReader()
Dim customerId As String = ser (“customerId”)
Dim deviceIdAs String = ser(“deviceId”)
next
</ code> </ pre>

我使用'Chetan Sanghani'答案时收到错误 字符串ex [String] </ p>
</ div>周围的括号

展开原文

原文

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]

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐