在 VB.NET 中将 JSON 转换为数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23079275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:23:05  来源:igfitidea点击:

Converting JSON to Array in VB.NET

vb.net

提问by Professor Haseeb

I have this code

我有这个代码

Dim x As String
x = "{'books':[{'title':'HarryPotter','pages':'134'}]}"

what i want to do is to convert it into array like we do in PHP using the json_decode(x,TRUE or FALSE)function

我想要做的是使用json_decode(x,TRUE or FALSE)函数将它转换为数组,就像我们在 PHP 中所做的那样

回答by sloth

Your string xdoes not contain an array, but a single JSON object.

您的字符串x不包含数组,而是包含单个 JSON 对象。

Just use a JSON library like Json.NETto parse your string:

只需使用像Json.NET解析字符串这样的 JSON 库:

Dim x = "{'books':[{'title':'HarryPotter','pages':'134'}]}"

Dim result = JsonConvert.DeserializeObject(x)
Console.WriteLine(result("books")(0)("title") & " - " & result("books")(0)("pages"))

Output:

输出:

HarryPotter - 134

哈利波特 - 134

回答by Deja Vu

@Professor Haseeb Maybe you forget to Add the following to @Dominic Kexel solution:

@Professor Haseeb 也许您忘记将以下内容添加到@Dominic Kexel 解决方案中:

Imports Newtonsoft.Json

Or use:

或使用:

Dim result = Newtonsoft.Json.JsonConvert.DeserializeObject(x)