vb.net 使用 Visual Basic 2010 中的对象进行 JSON 编码和解码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18965671/
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 15:12:01  来源:igfitidea点击:

JSON Encode and Decode with objects in Visual Basic 2010

jsonvb.netvisual-studio-2010linqjson.net

提问by Blease

I have the following code:

我有以下代码:

Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Module Module1

Structure JSONList
    Dim Name, Email As String
    Dim Age As Integer
End Structure

Sub Main()
    Dim Data(1) As JSONList

    Data(0).Name = "Josh"
    Data(0).Age = 17
    Data(0).Email = "[email protected]"
    Data(1).Name = "Greg"
    Data(1).Age = 17
    Data(1).Email = "[email protected]"

    Dim JSONEncode As String
    JSONEncode = JsonConvert.SerializeObject(Data)
    Console.WriteLine(JSONEncode)
    Console.WriteLine()
    Console.WriteLine()

    Dim JSONDecode() As JSONList = JsonConvert.DeserializeObject(JSONEncode)
    Console.WriteLine(JSONDecode(0).Name)

    Console.ReadKey()


End Sub

End Module

The first encoding part of the script is used to store the encoded string to a database, the output is:

脚本的第一个编码部分用于将编码后的字符串存储到数据库中,输出为:

[{"Name":"Josh","Email":"[email protected]","Age":17},{"Name":"Greg","Email":"[email protected]","Age":17}]

Now when I try to decode this JSON string, I get an error Unable to cast object of type 'Newtonsoft.Json.Linq.JArray' to type 'JSONList[]'.

现在,当我尝试解码此 JSON 字符串时,出现错误 Unable to cast object of type 'Newtonsoft.Json.Linq.JArray' to type 'JSONList[]'.

I need the data to be encoded in the JSON format so that I can use it in my website that uses PHP to decode it. I am using Visual Basic 2010 along with JSON.NET.

我需要以 JSON 格式对数据进行编码,以便我可以在使用 PHP 对其进行解码的网站中使用它。我正在使用 Visual Basic 2010 和 JSON.NET。

回答by Adrian Wragg

The problem is that JsonConvert.DeserializeObjectdeserialises into an object of type Newtonsoft.Json.Linq.JArraywhich .net cannot automatically convert to an array of JSONList. A little bit of conversion is required:

问题是JsonConvert.DeserializeObject反序列化为Newtonsoft.Json.Linq.JArray.net 无法自动转换为JSONList. 需要一点转换:

Dim jsonObject As Newtonsoft.Json.Linq.JArray =
                                        JsonConvert.DeserializeObject(JSONEncode)

Dim JSONDecode() As JSONList = (
                                 From j In jsonObject
                                 Select New JSONList() With {.Age = j("Age"),
                                                             .Email = j("Email"),
                                                             .Name = j("Name")}
                               ).ToArray()

回答by Brian Rogers

As @Adrian said, JsonConvert.DeserializeObjectwill deserialize to JArray, which .Net cannot automatically convert into an array of your JSONListstructure. However, you don't need to do manual conversion; you simply need to use the overload of DeserializeObjectwhich accepts a type parameter. This will allow Json.Net to deserialize directly to your type without needing any special conversion code.

正如@Adrian 所说,JsonConvert.DeserializeObject将反序列化为JArray.Net 无法自动转换为您的JSONList结构数组。但是,您不需要进行手动转换;您只需要使用DeserializeObject接受类型参数的重载。这将允许 Json.Net 直接反序列化为您的类型,而无需任何特殊的转换代码。

Dim JSONDecode() As JSONList = _
                 JsonConvert.DeserializeObject(Of JSONList())(JSONEncode)