如何使用 vb.net 创建 JSON 数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9563748/
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
How to create JSON array using vb.net
提问by Bader
How to create this JSONarray using vb.netarray
如何JSON使用vb.net数组创建此数组
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"}
]};
回答by Josua Marcel Chrisano
As you're using .NET 2.0 you have to use the JSON libary by James, with download at Codeplex (version for .NET 2.0).
当您使用 .NET 2.0 时,您必须使用 James 的 JSON 库,并在 Codeplex(.NET 2.0 版本)上下载。
An example of using Json.NET
一个使用 Json.NET 的例子
Add a reference to Newtonsoft.Json, and an Import Newtonsoft.Jsonin your class.
在类中添加对Newtonsoft.Json的引用和Import Newtonsoft.Json。
Example:
例子:
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)
The output variable will hold the value:
输出变量将保存值:
{
"Name": "Captopril",
"Expiry": "\/Date(1230375600000+1300)\/",
"Price": 3.99,
"Sizes": [
"Small",
"Medium",
"Large"
]
}
回答by Andrea Colabufalo
Public Class Student
Public Property value As String
Public Property name As Integer
End Class
On Page_Load:
在 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)
回答by LongArm
Check out the visual studio gallery extension called JSON.net or go to their codeplex page (JSON on codeplex)
查看名为 JSON.net 的 Visual Studio 画廊扩展或转到他们的 codeplex 页面(codeplex 上的JSON)

