2013-09-22 119 views
0

我有一個Newtonsoft.Json的問題。我試圖解析來自URL的JSON,但我得到一個錯誤。這裏是JSON:Newtonsoft.Json.JsonReaderException

[ 
    { 
     "ID": "0", 
     "Nome": "we", 
     "Data": "2013-09-16", 
     "Orario": "00:00:16", 
     "Prestazione": "dfg", 
     "Stato": "dfg", 
     "Numero_Telefono": "dfg" 
    }, 
    { 
     "ID": "0", 
     "Nome": "fg", 
     "Data": "2013-09-26", 
     "Orario": "00:00:00", 
     "Prestazione": "", 
     "Stato": "", 
     "Numero_Telefono": "" 
    }, 
    { 
     "ID": "1", 
     "Nome": "davide", 
     "Data": "2013-09-26", 
     "Orario": "00:00:16", 
     "Prestazione": "ds", 
     "Stato": "sd", 
     "Numero_Telefono": "3546" 
    } 
] 

這裏是我使用的代碼:

Dim request As HttpWebRequest 
Dim response As HttpWebResponse = Nothing 
Dim reader As StreamReader 

Try 

    request = DirectCast(WebRequest.Create("http://nhd.altervista.org/connectDb.php"), HttpWebRequest) 
    response = DirectCast(request.GetResponse(), HttpWebResponse) 
    reader = New StreamReader(response.GetResponseStream()) 

    Dim rawresp As String 
    rawresp = reader.ReadToEnd() 

    Dim jResults As JObject = JObject.Parse(rawresp) 
    Dim results As List(Of JToken) = jResults.Children().ToList() 

    For Each item As JProperty In results 
     item.CreateReader() 
     MsgBox(item.Value("img")) ' because my tag in json is img 
    Next 

Catch ex As Exception 
    Console.WriteLine(ex.ToString) 
    MsgBox(ex.ToString) 
Finally 
    If Not response Is Nothing Then response.Close() 
End Try 

這是我收到的錯誤,當我嘗試解析JSON:

Newtonsoft.Json.JsonReaderException: Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 1, position 1. 

你能幫我解決這個問題?

回答

3

由於您使用的是JObject.Parse,但您的JSON包含一個數組,因此您會收到此錯誤消息。要解決此問題,請改爲使用JArray.Parse

但是,還有一個問題:其餘的代碼沒有設置爲正確處理結果。由於您的結果是一組對象,因此您的For Each循環需要預計爲JObject項目,而不是JProperty項目。一旦你有每個項目,你可以根據需要從它們獲取屬性。

我不確定你在用item.CreateReader()這條線做什麼,因爲你沒有對它的返回值做任何事情,而且你似乎也不需要。同樣,我也對你的MsgBox(item.Value("img"))行感到困惑,因爲在JSON的任何地方都沒有「img」屬性。所以這將永遠是空的。

這是一些更正的代碼,它將解析JSON並顯示結果中每個對象的所有屬性。這應該給你一個合作的起點。

Dim request As HttpWebRequest 
Dim response As HttpWebResponse = Nothing 
Dim reader As StreamReader 

Try 

    request = DirectCast(WebRequest.Create("http://nhd.altervista.org/connectDb.php"), HttpWebRequest) 
    response = DirectCast(request.GetResponse(), HttpWebResponse) 
    reader = New StreamReader(response.GetResponseStream()) 

    Dim rawresp As String 
    rawresp = reader.ReadToEnd() 

    Dim jResults As JArray = JArray.Parse(rawresp) 
    Dim results As List(Of JToken) = jResults.Children().ToList() 

    For Each item As JObject In results 
     Dim demo As String = "" 
     For Each prop As JProperty In item.Properties() 
      demo = demo + prop.Name + " = " + prop.Value.ToString() + vbCrLf 
     Next 
     MsgBox(demo) 
    Next 

Catch ex As Exception 
    Console.WriteLine(ex.ToString) 
    MsgBox(ex.ToString) 
Finally 
    If Not response Is Nothing Then response.Close() 
End Try 
+0

這對JSON解析器非常有幫助我在寫謝謝,c#基本上是完全一樣的。 –

+0

@ZachM。很高興你發現它很有用。 –

相關問題