我对Json解析还很陌生。我现在想从我的Json(请参阅下文)中提取“ id”(在数据部分中显示)值。
{
"data": [{
"date": "2018-10-10 10:50:00",
"vehicle_id": 11,
"accept_phone": true,
"phone": "070000000",
"accept_email": true,
"email": "abe@abeprakash.com",
"details": "Special Very",
"user_id": "3378",
"id": 11513,
"team_id": 2806,
"user_info": {
"id": 3378,
"name_first": "abe",
"name_last": "prakash",
"name_full": "abe prakash ",
"email": "abe@abeprakash.com",
"phone": "070000000",
"ratings_count": 0
},
"recipient": {
"name": "Tim ",
"email": "Dev@journease.co.uk",
"phone": "+44 (0)117 9586 180"
},
"footer_text": "",
"invoice_logo": "",
"including_vat": 0
},
"distance_in_miles": 0,
"vehicle": {
"id": 11,
"name": "Abe Car",
"icon": "\/assets\/uploads\/vehicles\/11.svg",
"sort_no": 8,
"size": 800
}
}
],
"properties": []
}
我一直在尝试,这是我走了多远,不确定这是否是最好的方法
尝试1 (topLevelResponse没什么,但是json字符串中的id有一个值)
Dim responseReader As New StreamReader(response.GetResponseStream())
'Use the below variable to see the rawJson
Dim rawJsonResponseString = ""
Using rawJsonResponseStream As Stream = response.GetResponseStream()
Dim reader As StreamReader = New StreamReader(rawJsonResponseStream, Encoding.UTF8)
rawJsonResponseString = reader.ReadToEnd()
End Using
Dim topLevelResponse As JObject = JObject.Parse(rawJsonResponseString)("data").SelectToken("id")
尝试2 (我不想循环查找ID,因为将来在json响应中可能会加载更多项目)
Dim token As JToken = CType(topLevelResponse.SelectToken("data"), JArray)
任何帮助都是值得赞赏的家伙。你们可能已经发现,我正在使用VB.net和Json.net。谢谢。
尝试3 (我得到了值,但现在确定将值硬编码到哪里是个好主意,尽管在数据中始终有一个位置)
Dim提取的ID = JObject.Parse(rawJsonResponseString).SelectToken(“ data [0] .id”)
答案 0 :(得分:1)
这是有效的JSON:
{“ data”:[{“ date”:“ 2018-10-1010:50:00”,“ vehicle_id”:11,“ accept_phone”:true,“ phone”:“ 070000000”,“ accept_email”: true,“电子邮件”:“ abe@abeprakash.com”,“详细信息”:“ SpecialVery”,“ user_id”:“ 3378”,“ id”:11513,“ team_id”:2806,“ user_info”:{“ id” :3378,“ name_first”:“ abe”,“ name_last”:“ prakash”,“ name_full”:“ abeprakash”,“电子邮件”:“ abe@abeprakash.com”,“电话”:“ 070000000”,“ ratings_count” :0},“收件人”:{“名称”:“ Tim”,“电子邮件”:“ Dev@journease.co.uk”,“电话”:“ + 44(0)1179586180”},“ footer_text”:“ “,” invoice_logo“:”“,” includes_vat“:0,” distance_in_miles“:0,” vehicle“:{” id“:11,” name“:” AbeCar“,” icon“:” / assets / uploads / Vehicles / 11.svg“,” sort_no“:8,” size“:800}}],” properties“:[]}
由于它是无效的,所以我不知道您在尝试解析它时是如何得到错误的,或者您只是在此处错误地张贴了它。
回到主题,有多种方法,但这是您所要求的最短的方法。
可视化步骤:
{JSON}
{
"data":[+], '<<< ("data")
"properties":[+]
}
{
"data":[
{+} '<<< first, that is (0)
],
"properties":[+]
}
{
"data":[
{
"date":"2018-10-1010:50:00",
"vehicle_id":11,
"accept_phone":true,
"phone":"070000000",
"accept_email":true,
"email":"abe@abeprakash.com",
"details":"SpecialVery",
"user_id":"3378",
"id":11513, '<<< ("id")
"team_id":2806,
"user_info":{+},
"recipient":{+},
"footer_text":"",
"invoice_logo":"",
"including_vat":0,
"distance_in_miles":0,
"vehicle":{+}
}
],
"properties":[+]
}
Dim test As JObject = JObject.Parse(TextBox1.Text)
Dim test1 = test("data")(0)("id") 'test1 = 11513
您应该使用断点检查JObject,并查看格式看起来如何才能知道如何获得所需的内容。按数字排列,按名称(如果可以的话,按键)排列。
答案 1 :(得分:0)
到目前为止,根据堆栈成员的评论,我找到的最好方法是
Dim提取的ID = JObject.Parse(rawJsonResponseString).SelectToken(“ data [0] .id”)