2017-10-16 44 views
0

如何创建一个数组,如果我们转换成json,我们会得到下面的结果。在vb.net 2010中创建一个多维数组

{{"name":"first_example_1"}, {"name": "second_example_1"}} 

我试图用这个

dim x as jArray 
x.add("first_example_1") 
x.add("second_example_1") 

,但是当我尝试将上述以JSON我得到这个

{"firstname_example_1", "second_example_1"} 

我如何添加一个索引?

回答

1

试试这个

 Dim jArray(1) As Object 
     jArray(0) = New With {Key .name = "second_example_1"} 
     jArray(1) = New With {Key .name = "firstname_example_1"} 
     Dim serializer As New JavaScriptSerializer() 
     Dim result As String 
     result = serializer.Serialize(jArray) 

,不要忘了Imports System.Web.Script.Serialization

+0

我在阅读您的答案之前更改了我的代码。根据我的问题,你的代码工作,并产生我想要的确切结果,很酷。但是由于我的引用中没有system.web.script.serialization,所以我很难对这个对象进行反序列化(“因为我是VB.net上的新手”),我不知道为什么我可以'在我的参考资料上找到它,我只有system.web.services。谢谢你,先生。我很感激。 –

0

@styx作品上面的答案,但我已经改变我的代码。这个答案仅供参考。

我创建了一个名为PerInfo

public class PerInfo 
    public firstname 
    public lastname 
end class 

要序列化,我写这个类;

dim x as new PerInfo 
x.firstname = textbox1.text 
x.address = textbox2.text 

dim res as string = JsonConvert.SerializeObject(x) 
' the above code produces my desired result which is 
' {"firstname":"jeo","address":"GSC"} 

要反序列化,我做了这个;

Dim t As PerInfo = JsonConvert.DeserializeObject(Of PerInfo)(x) 
'I can now access the `firstname` and `address` via 
' t.firstname and t.address 

MsgBox(t.firstname & "===" & t.address) 

希望这有助于...

PS:我通过添加引用手动添加的Newtonsoft.Json.dll 版本Net 2.0为使用.net 2.0框架,如果我是对PC的向后兼容性。随意纠正我这一点。