简体   繁体   中英

VB.net JavaScriptSerializer how do I make this Json string?

I'm trying to create this JSON string but having problem creating it. I'm getting System.NullReferenceException error.

Here is the json string that I would like to create

{"id":["1","2","3","4"]},"sid":["s1","s2","s3","s4"]} 

Here is the code I have so far.

Imports System.Web.Services
Imports System.Data

Partial Class test_sidetab
    Inherits System.Web.UI.Page

    <WebMethod()> Public Shared Function getfav() As String
        Dim fav As New Favorites()
        Dim js As New System.Web.Script.Serialization.JavaScriptSerializer()
            Dim i As Integer = 1
            While i < 10
                fav.id.Add(i.ToString)
                fav.sid.Add("s"+i.ToString)
                i += 1
            End While
        Dim str As String = js.Serialize(fav)
        Return str
    End Function
End Class
Public Class Favorites

    Public id As ArrayList, sid As ArrayList

End Class

fav.id is null. You need to put an ArrayList into it by writing Public id As New ArrayList (or, better yet, List(Of String) )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM