2013-10-17 75 views
0

我想從一個http文章閱讀json字符串,我試圖通過手動填充json字符串來測試我的代碼,但我的問題是我無法添加相同的節點多次....換句話說,如果我把下面的工作原理:錯誤閱讀json字符串

Dim json As String = "{'name': 'jocelyne','mo': '70274724'} 

,但如果我把下面的這是行不通的:

Dim json As String = "{'name': 'jocelyne','mo': '70274724'},{'name': 'eliane','mo': '12345678'}" 

,我實際上是名稱的一百莫數字將發送給我一個json字符串

這是我的全部代碼:

Request.InputStream.Position = 0 'you need this else the magic doesn't happen 
    Dim inputStream As New StreamReader(Request.InputStream) 
    'Dim json As String = inputStream.ReadToEnd() 
    Dim json As String = "{'name': 'jocelyne','mo': '70274724'},{'name': 'eliane','mo': '12345678'}" 

    Dim jss As New System.Web.Script.Serialization.JavaScriptSerializer() 
    Dim dict As Dictionary(Of String, String) = jss.Deserialize(Of Dictionary(Of String, String))(json) 

    For Each item As KeyValuePair(Of String, String) In dict 
     Response.Write(item.Key & " - " & item.Value & "<br>") 
    Next 

怎麼能解決這個問題?我只是需要能夠讀取使用HTTP POST給我這個JSON格式和反序列化讀取數據

+0

如果我傳遞數組,我需要傳遞JSON字符串 – Vandesh

+0

@Vandesh中的 - 「[{}],[{}]」這樣的JSON對象數組:'無效的JSON基元:[{'name ':'eliane','mo':'12345678'}]。'當初始化字典時 – User7291

回答

0

有必要把整串,以括號

Dim serializer As New Web.Script.Serialization.JavaScriptSerializer() 
Dim json As String = "[{'name': 'jocelyne','mo': '70274724'},{'name': 'eliane','mo': '12345678'}]" 
Dim tst = serializer.Deserialize(Of Object)(json) 

結果是對象數組。