繁体   English   中英

如何使用vb.net创建JSON数组

[英]How to create JSON array using vb.net

如何使用vb.net数组创建此JSON数组

var data = {items: [
{value: "21", name: "Mick Jagger"},
{value: "43", name: "Johnny Storm"},
{value: "46", name: "Richard Hatch"},
{value: "54", name: "Kelly Slater"},
{value: "55", name: "Rudy Hamilton"},
{value: "79", name: "Michael Jordan"}
]};

当您使用.NET 2.0时,您必须使用James的JSON库,并在Codeplex下载(.NET 2.0版本)。

使用Json.NET的一个例子

添加对Newtonsoft.Json的引用,并在您的类中添加Import Newtonsoft.Json

例:

Import Newtonsoft.Json

Dim product As New Product()
product.Name = "Captopril"
product.Expiry = New DateTime(2008, 12, 28)
product.Price = 3.99D
product.Sizes = New String() {"Small", "Medium", "Large"}


'Call SeralizeObject to convert the object to JSON string'
Dim output As String = JavaScriptConvert.SerializeObject(product)

输出变量将保存值:

{
  "Name": "Captopril",
  "Expiry": "\/Date(1230375600000+1300)\/",
  "Price": 3.99,
  "Sizes": [
    "Small",
    "Medium",
    "Large"
  ]
}
Public Class Student
    Public Property value As String
    Public Property name As Integer
End Class

在Page_Load上:

'creating sample student ojects
    Dim obj1 As New Student() With {.value = "Mick Jagger", .name = 21}
    Dim obj2 As New Student() With {.value = "Johnny Storm", .name = 43}
    Dim obj3 As New Student() With {.value = "Richard Hatch", .name = 46}
    Dim obj4 As New Student() With {.value = "Kelly Slater", .name = 54}
    'adding student objects to list
    Dim objStudentList As New List(Of Student)() From { obj1,obj2, obj3, obj4}

    Dim objJSSerializer As New System.Web.Script.Serialization.JavaScriptSerializer()

    'Serialization .NET Object to JSON
    strJSON = objJSSerializer.Serialize(objStudentList)

    Dim csname2 As String = "ButtonClickScript"
    Dim cstype As Type = Me.GetType()
    Dim cstext2 As New StringBuilder()
    Dim cs As ClientScriptManager = Page.ClientScript
    cstext2.Append("<script type=""text/javascript""> var data = {items: " + strJSON)
    cstext2.Append(" }; </")
    cstext2.Append("script>")
    cs.RegisterClientScriptBlock(cstype, csname2, cstext2.ToString(), False)

查看名为JSON.net的visual studio gallery扩展或转到他们的codeplex页面(Codeplex上的JSON

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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