将具有 属性 名称作为数字的 JSON 对象反序列化为 VB.NET 中的对象

Deserialize JSON object which has property name as number into a object in VB.NET

我有以下 JSON 个对象

myvalues : {
     0 : "value0",
     1 : "value1",
     2 : "value2",
     3 : "value3"
}

我想将此 JSON 对象绑定到 vb.net class 对象作为 WCF OperationContract 方法的输入 - 但我无法定义数字属性 名称为数字。我收到错误消息:

Identifier expected on property names because property name cannot be a number

使用以下内容:

Public class myvalues_class

    public property 0 as string
    public property 1 as string
    public property 2 as string
    public property 3 as string

end class

如何将此 JSON 对象转换为 vb.net 对象 class?

uses DataContractJsonSerializer, so you will need to annotate your Values type with data contract attributes 将某些 validly-named vb.net 属性映射到 JSON 数字 属性 名称,例如像这样:

<System.Runtime.Serialization.DataContract> _
Public Class Values
    <DataMember(Name:="0")> _
    Public Property r As Integer

    <DataMember(Name:="1")> _
    Public Property g As Integer

     <DataMember(Name:="2")> _
    Public Property b As Integer

    <DataMember(Name:="3")> _
    Public Property a As Integer
End Class

请注意,数据协定序列化器是 opt-in so you will have to mark all properties to be serialized with DataMemberAttribute