Vb.net反序列化json [英] Vb.net deserialize json
本文介绍了Vb.net反序列化json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
HI,我有一个包含以下内容的文件API2.txt:
{
results:[{
rating2 :6.66666,
downloads_min:5000000,
klm:[{
top1:mine
},
{
top2:你的
}]
},
{
rating2:4.4444,
downloads_min:505550000,
klm :[{
top1:2mine
},
{
top2:2yours
}]
},
{
rating2:900000,
downloads_min:505550000,
klm:[{
top1:mine
} ,
{
top2:你的
}]
}],
number_results:263,
has_next:true,
page:1,
num_pages:132,
限制:2
}
我可以获得除顶部之外的任何键的值1和top2。如何获得top1和top2的值?我使用以下代码提取:
我尝试过:
Dim 值作为 String = File.ReadAllText( C:\ Farmers\API\API2.txt )
Dim json As JObject = JObject.Parse(value)
对于 每个 Row2 在 json( results)。ToList()
Response.Write(Row2 ( rating2)。ToString()& < br>)
下一步
Response.Write (json( number_results))
解决方案
< blockquote>我有一个帮助函数,用于从我在文章中使用的JSON中提取数据:在C#中使用JSON& VB [ ^ ]
Imports Newtonsoft.Json.Linq
< span class =code-keyword>公共 模块 JsonExtensions
< Extension>
公共 功能 FindTokens(containerToken As JToken,name As String )_
作为列表( JToken)
Dim matches = 新列表( JToken)()
FindTokens(containerToken,name,matches)
返回匹配
结束 函数
私有 Sub FindTokens(containerToken 作为 JToken,名称作为 字符串,_
匹配作为列表( JToken))
如果 containerToken.Type = JTokenType。[对象] 那么
对于 每个子作为 JProperty 在 containerToken.Children( of JProperty)()
如果 child.Name = name 那么
matches.Add(child.Value)
结束 如果
FindTokens(child.Value,name,matches)
Next
ElseIf containerToken.Type = JT okenType.Array 然后
对于 每个 child 作为 JToken 在 containerToken.Children()
FindTokens(子,名,匹配) )
下一步
结束 如果
结束 Sub
结束 模块
然后使用,你只需做一些类似::
Dim 标签 As List( Single )= _
JObject.Parse(rawJSON)_
.FindTokens( rating2)_
.Values( Single )()_
.ToList()
更新:以下是您可以插入的完整示例进入测试控制台应用程序:
Imports System.Runtime.CompilerServices
Imports Newtonsoft.Json.Linq
Module Module1
Sub Main()
Dim rawJSON = {
results:[{
rating2:6.66666,
downloads_min :5000000,
klm:[{
top1:我的
},
{
top2 :你的
}
},
{
rating2:4.4444,
downloads_min:505550000,
klm:[{
top1:2mine
},
{
top2:2yours
}]
},
{
rating2:900000,
downloads_min:505550000,
klm:[{
top1 :我的
},
{
top2:你的
}]
}],
number_results:263,
has_next:true,
page:1,
num_pages:132,
limit:2
}
Dim 标签作为列表( Single )=
JObject.Parse(rawJSON)_
.FindTokens( rating2)_
.Values( of 单个)()_
.ToList()
对于 每个标签在标签中
Console.WriteLine(tag)
下一步
结束 Sub
结束 模块
公开 模块 JsonExtensions
< Extension>
公共 功能 FindTokens(containerToken As JToken,name As String )_
作为列表( JToken)
Dim matches = 新列表( JToken)()
FindTokens(containerToken,name,matches)
返回匹配
结束 函数
私有 Sub FindTokens(containerToken 作为 JToken,名称作为 字符串,
匹配作为列表( JToken))
如果 containerToken.Type = JTokenType。[对象] 那么
对于 每个子作为 JProperty 在 containerToken.Children( of JProperty)()
如果 child.Name = name 那么
matches.Add(child.Value)
结束 如果
FindTokens(child.Value,name,matches)
Next
ElseIf containerToken.Type = JTok enType.Array 然后
对于 每个 child 作为 JToken 在 containerToken.Children()
FindTokens(子,名,匹配) )
下一步
结束 如果
结束 Sub
结束 模块
输出为:
6.66666
4.4444
900000
感谢您的回复。但我一直得到:
FindTokens不是Newtonsoft.Json.Linq的成员。
我是不确定我做错了什么。 deserializtion来自按钮点击事件。
所以当我调用FindTokens函数时,我得到了这个错误。
谢谢,
HI, I have a file API2.txt that contain the following:
{
"results": [{
"rating2": 6.66666,
"downloads_min": 5000000,
"klm": [{
"top1": "mine"
},
{
"top2": "yours"
}]
},
{
"rating2": 4.4444,
"downloads_min": 505550000,
"klm": [{
"top1": "2mine"
},
{
"top2": "2yours"
}]
},
{
"rating2": 900000,
"downloads_min": 505550000,
"klm": [{
"top1": "mine"
},
{
"top2": "yours"
}]
}],
"number_results": 263,
"has_next": true,
"page": 1,
"num_pages": 132,
"limit": 2
}
I can get the value of any keys except "top1" and "top2". how can I get the value for "top1" and "top2"? I am using the following code to extract:
What I have tried:
Dim value As String = File.ReadAllText("C:\Farmers\API\API2.txt")
Dim json As JObject = JObject.Parse(value)
For Each Row2 In json("results").ToList()
Response.Write(Row2("rating2").ToString() & "<br>")
Next
Response.Write(json("number_results"))
解决方案
I have a helper function for pulling data from JSON that I use in my article: Working with JSON in C# & VB[^]
Imports Newtonsoft.Json.Linq Public Module JsonExtensions <Extension> Public Function FindTokens(containerToken As JToken, name As String) _ As List(Of JToken) Dim matches = New List(Of JToken)() FindTokens(containerToken, name, matches) Return matches End Function Private Sub FindTokens(containerToken As JToken, name As String, _ matches As List(Of JToken)) If containerToken.Type = JTokenType.[Object] Then For Each child As JProperty In containerToken.Children(Of JProperty)() If child.Name = name Then matches.Add(child.Value) End If FindTokens(child.Value, name, matches) Next ElseIf containerToken.Type = JTokenType.Array Then For Each child As JToken In containerToken.Children() FindTokens(child, name, matches) Next End If End Sub End Module
Then to use, you simply do something like::
Dim tags As List(Of Single) = _ JObject.Parse(rawJSON) _ .FindTokens("rating2") _ .Values(Of Single)() _ .ToList()
UPDATE: Here is a full example for you that you can plug into a test console app:
Imports System.Runtime.CompilerServices Imports Newtonsoft.Json.Linq Module Module1 Sub Main() Dim rawJSON = "{ ""results"": [{ ""rating2"": 6.66666, ""downloads_min"": 5000000, ""klm"": [{ ""top1"": ""mine"" }, { ""top2"": ""yours"" }] }, { ""rating2"": 4.4444, ""downloads_min"": 505550000, ""klm"": [{ ""top1"": ""2mine"" }, { ""top2"": ""2yours"" }] }, { ""rating2"": 900000, ""downloads_min"": 505550000, ""klm"": [{ ""top1"": ""mine"" }, { ""top2"": ""yours"" }] }], ""number_results"": 263, ""has_next"": true, ""page"": 1, ""num_pages"": 132, ""limit"": 2 }" Dim tags As List(Of Single) = JObject.Parse(rawJSON) _ .FindTokens("rating2") _ .Values(Of Single)() _ .ToList() For Each tag In tags Console.WriteLine(tag) Next End Sub End Module Public Module JsonExtensions <Extension> Public Function FindTokens(containerToken As JToken, name As String) _ As List(Of JToken) Dim matches = New List(Of JToken)() FindTokens(containerToken, name, matches) Return matches End Function Private Sub FindTokens(containerToken As JToken, name As String, matches As List(Of JToken)) If containerToken.Type = JTokenType.[Object] Then For Each child As JProperty In containerToken.Children(Of JProperty)() If child.Name = name Then matches.Add(child.Value) End If FindTokens(child.Value, name, matches) Next ElseIf containerToken.Type = JTokenType.Array Then For Each child As JToken In containerToken.Children() FindTokens(child, name, matches) Next End If End Sub End Module
And the output is:
6.66666 4.4444 900000
Thanks for the reply. But I keep getting:
FindTokens is not member of Newtonsoft.Json.Linq.
I am not sure what I am doing wrong. The deserializtion is from a button click event.
so when I call the FindTokens function I get that error.
Thanks,
这篇关于Vb.net反序列化json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文