2017-10-16 3 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

답변을 읽기 전에 코드를 변경했습니다. 내 질문에 따라 귀하의 코드가 작동하고 멋지게 원하는 결과를 생성합니다. 하지만 힘든 시간을 역 직렬화하는 데 문제가 있었는데 ("VB.net의 초보자"로 인해) 나는 내 참조에서 system.web.script.serialization을 가지고 있지 않기 때문에 왜 내가 할 수 있는지 모르겠다. 내 참조에 그것을 찾지, 난 단지 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 : 나는 수동으로 내가 옳다 경우 .net 2.0 프레임 워크를 사용하여 PC의 이전 버전과의 호환성을 위해 추가 참조를 통해 Newtonsoft.Json.dll 버전 Net 2.0을 추가했다. 이걸로 나를 고쳐 주시기 바랍니다.