本文介绍了需要一个类/结构来序列化 VB.net 中的 JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出一个类/结构来处理以下 JSON 格式:

I am trying to figure out a class/struture to handle the following JSON format:

{
"ReturnData": [
    {
        "id": "msg2DoesNotExistName",
        "value": "value 1",
        "userExists": 2 
    },
    {
        "id": "msg2DoesNotExistName",
        "value": "Value 2",
        "userExists": 2 
    } 
],
"SetValue": [
    {
        "id": "msg2DoesNotExistName",
        "value": "value 1" 
    },
    {
        "id": "msg2DoesNotExistName",
        "value": "Value 2" 
    } 
]

}

我已经尝试过(只是 SetValue 部分):

I have tried (just the SetValue portion):

    <Serializable()> _
Public Class Stuff
            Public SetValue() As ArrayList
End Class

Public Function TestSerial3(ByVal somevar As String) As String
    Dim s As New JavaScriptSerializer()
    Dim ret As String
    Dim b As New SaveType()
   Dim p1 As New Stuff
    b = New SaveType
    b.id = "ctl00_number_1"
    b.value = "Test1"
    p1.SetValue(0).Add(b)

    b = New SaveType
    b.id = "ctl100_number_2"
    b.value = "Test2"
    p1.SetValue(1).Add(b)

    ret = s.Serialize(p1)

    return ret
 end function

结果如下:System.NullReferenceException:未将对象引用设置为对象的实例.

This is the result: System.NullReferenceException: Object reference not set to an instance of an object.

我能够使用结构序列化内部部分,但无法弄清楚如何在不诉诸字符串构建的情况下包含外部名称(ReturnData、SetValue):

I am able to serialize the inside portion using a structure, but cannot figure out how to include the outer name (ReturnData, SetValue) without resorting to string building:

<Serializable()> _
Public Structure UserExistsType
    Public id As String
    Public value As String
    Public userExists As Integer
End Structure

 Dim b(1) As UserExistsType
 b(0).id = "msg2DoesNotExistName"
 b(0).value = "value 1"
 b(0).userExists = 2
 b(1).id = "msg2DoesNotExistName"
 b(1).value = "Value 2"
 b(1).userExists = 2
 ret = s.Serialize(b)
 ret = "{" & Chr(34) & "ReturnData" & Chr(34) & ":" & ret & "}"

我可能有也可能没有 ReturnData 和 SetValue 的数据(至少一个或两个).我试图让序列化程序处理大部分格式,而不必检查空部分和单项数组.有什么建议吗?

I may or may not have Data for ReturnData and SetValue (one or both at a minimum). I am trying to let the serializer handle most of the formatting without having to check for empty sections and single-item arrays. Any suggestions?

推荐答案

这样的事情如何(我只编写了 SetValue,你可以使用相同的技术轻松添加 ReturnData.

How bout something like this (I only coded up SetValue, you can easily add ReturnData using the same technique.

本质上,只需将两个数组包装在一个外部对象中(我称之为 MethodCall,因为它看起来像这样).

Essentially, just wrap your two arrays in an outer object (I called mine MethodCall, because that's what it looks like).

    Imports System.Runtime.Serialization
    Imports System.Web.script.serialization

    Public Class Form1

        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Debug.Print(TestSerial3())
        End Sub


        Public Function TestSerial3() As String
            Dim s As New JavaScriptSerializer()
            Dim ret As String
            Dim r = New MethodCall
            r.SetValue(0) = New SetValue
            With r.SetValue(0)
                .id = "ctl00_number_1"
                .value = "Test1"
            End With


            r.SetValue(0) = New SetValue
            With r.SetValue(0)
                .id = "ctl100_number_2"
                .value = "Test2"
            End With
            ret = s.Serialize(r)

            Return ret
        End Function

    End Class


    <Serializable()> _
    Public Class ReturnData
        Public id As String
        Public value As String
        Public userExists As Integer
    End Class


    <Serializable()> _
    Public Class SetValue
        Public id As String
        Public value As String
    End Class


    <Serializable()> _
    Public Class MethodCall
        Public SetValue(1) As SetValue
        Public returnData(2) As ReturnData
    End Class

这篇关于需要一个类/结构来序列化 VB.net 中的 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 21:20