如何解决读取 REST API JSON 回复
我一直在网络上来回搜索,但找不到我的问题的提示。
我正在通过 RestSharp Client
调用 REST API。我检索了这样的回复:
{
"meta": {
"query_time": 0.007360045,"pagination": {
"offset": 1,"limit": 100,"total": 1
},"powered_by": "device-api","trace_id": "a0d33897-5f6e-4799-bda9-c7a9b5368db7"
},"resources": [
"1363bd6422274abe84826dabf20cb6cd"
],"errors": []
}
我现在想查询 resources
的值。这是我使用的代码:
Dim id_request = New RestRequest("/devices/queries/devices/v1?filter=" + filter,Method.GET)
id_request.AddHeader("Accept","application/json")
id_request.AddHeader("Authorization","bearer " + bearer)
Dim data_response = data_client.Execute(id_request)
Dim data_response_raw As String = data_response.Content
Dim raw_id As JObject = JObject.Parse(data_response_raw)
Dim id = raw_id.GetValue("resources").ToString
不幸的是,我只收到了 ["1363bd6422274abe84826dabf20cb6cd"]
而不是 1363bd6422274abe84826dabf20cb6cd
谁能指出我正确的方向?
我也尝试过使用 JsonConvert.DeserializeObject()
进行反序列化,但不知何故失败了。
我在这里找到了这个解决方案,但是如果我尝试重建它会失败,因为它无法识别字典部分
Dim tokenJson = JsonConvert.SerializeObject(tokenJsonString)
Dim jsonResult = JsonConvert.DeserializeObject(Of Dictionary(Of String,Object))(jsonString)
Dim firstItem = jsonResult.Item("data").Item(0)
编辑:
当尝试按照建议反序列化根目录但似乎第二个响应是嵌套的 JSON 时。
我有这样的回复:
dr = {
"meta": {
"query_time": 0.004813129,"trace_id": "5a355c86-37f7-416d-96c4-0c8796c940fc"
},"resources": [
{
"device_id": "1363bd6422274abe84826dabf20cb6cd","policies": [
{
"policy_type": "prevention","policy_id": "1d34205a4e2c4d1991431c037c8e5734","applied": true,"settings_hash": "7cb00a74","assigned_date": "2021-02-22T13:56:37.759459481Z","applied_date": "2021-02-22T13:57:19.962692301Z","rule_groups": []
}
],"meta": {
"version": "352"
}
}
],"errors": []
}
我尝试过:
Dim restApiResponse = JsonConvert.DeserializeObject(Of RestApiResponseRoot)(dr)
' This is your array of strings
Dim resources = restApiResponse.Resources
不幸的是我得到 Newtonsoft.Json.JsonReaderException: '解析值时遇到意外字符:{.路径“资源”,第 8 行,位置 3。'
解决方法
资源属性是一个数组。像往常一样,您需要指定要考虑的数组元素。在这种情况下,第一个,即索引0
处的元素。
Dim jsonObject = JObject.Parse(data_response_raw)
Dim firstResource = jsonObject("resources")(0).ToString()
如果您希望将数组内容作为字符串数组,而不仅仅是第一个元素 - 假设 resources
可以包含多个字符串(毕竟它是一个数组) - 反序列化为 String()
:
Dim jsonObject = JObject.Parse(data_response_raw)
Dim resources = JsonConvert.DeserializeObject(Of String())(jsonObject("resources").ToString())
如果您需要整个 JSON 响应,我建议反序列化为表示 JSON 的类 Model:
Public Class RestApiResponseRoot
Public Property Meta As Meta
Public Property Resources As List(Of String)
Public Property Errors As List(Of Object)
End Class
Public Class Meta
<JsonProperty("query_time")>
Public Property QueryTime As Double
Public Property Pagination As Pagination
<JsonProperty("powered_by")>
Public Property PoweredBy As String
<JsonProperty("trace_id")>
Public Property TraceId As Guid
End Class
Public Class Pagination
Public Property Offset As Long
Public Property Limit As Long
Public Property Total As Long
End Class
然后您可以反序列化模型的根对象 - 此处名为 RestApiResponseRoot
的类 - 并像往常一样访问其属性:
Dim restApiResponse = JsonConvert.DeserializeObject(Of RestApiResponseRoot)(
data_response_raw
)
' This is your array of strings
Dim resources = restApiResponse.Resources
另一个 JSON 响应略有不同,响应属性包含一个对象数组而不是字符串。
添加了更多属性和嵌套对象。您只需要调整模型。
Public Class RestApiResponseRoot2
Public Property Meta As RootObjectMeta
Public Property Resources As List(Of Resource)
Public Property Errors As List(Of Object)
End Class
Public Class RootObjectMeta
<JsonProperty("query_time")>
Public Property QueryTime As Double
<JsonProperty("powered_by")>
Public Property PoweredBy As String
<JsonProperty("trace_id")>
Public Property TraceId As Guid
End Class
Public Class Resource
<JsonProperty("device_id")>
Public Property DeviceId As String
Public Property Policies As List(Of Policy)
Public Property Meta As ResourceMeta
End Class
Public Class ResourceMeta
Public Property Version As String
End Class
Public Class Policy
<JsonProperty("policy_type")>
Public Property PolicyType As String
<JsonProperty("policy_id")>
Public Property PolicyId As String
Public Property Applied As Boolean
<JsonProperty("settings_hash")>
Public Property SettingsHash As String
<JsonProperty("assigned_date")>
Public Property AssignedDate As DateTimeOffset
<JsonProperty("applied_date")>
Public Property AppliedDate As DateTimeOffset
<JsonProperty("rule_groups")>
Public Property RuleGroups As List(Of Object)
End Class
Dim restApiResponse2 = JsonConvert.DeserializeObject(Of RestApiResponseRoot2)(dr)
Dim resources As List(Of Resource) = restApiResponse2.Resources
' DeviceId of the first Resources object
Dim deviceId = resources(0).DeviceId
您可以使用一些在线资源来处理您的 JSON 对象:
QuickType - JSON 到 .Net 类 - C#,没有 VB.Net
JSON Utils - JSON 到 .Net 类 - 包括 VB.Net。比 QuickType 功能稍差。
,尝试在输出的第一个和最后一个字符中使用 " 字符修剪资源值
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。