錯誤說: Error reading JArray from JsonReader. Current JsonReader item is not an array: null.
Json:
{
"Status": "1",
"Result": {
"Response": "Ok",
"TotalRecord": 2
},
"List": null
}
Code:
Public Function PostList()
Try
Dim rte As String = sr.ReadToEnd()
Dim jsonObject As JObject = JObject.Parse(rte)
Dim listArray As String = JsonConvert.SerializeObject(jsonObject.Item("List"))
Dim jsonArray As JArray = JArray.Parse(listArray) '在這一句有錯誤
If IsNothing(jsonArray) Or String.IsNullOrEmpty(jsonArray) Then
Else
For j = 0 To jsonArray.Count - 1
Dim id As String = Replace(Newtonsoft.Json.JsonConvert.SerializeObject(jsonArray(j).Item("ID")), """", "")
Dim name As String = Replace(Newtonsoft.Json.JsonConvert.SerializeObject(jsonArray(j).Item("name")), """", "")
Dim phoneNo As String = Replace(Newtonsoft.Json.JsonConvert.SerializeObject(jsonArray(j).Item("phoneNo")), """", "")
Next
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Function
我嘗試在代碼中添加 string.isnullorempty
來傳遞 null,但它不起作用。有時候Json會回傳[]
,所以加了IsNothing
,但是沒有問題。它在這一句有錯誤 Dim jsonArray As JArray = JArray.Parse(listArray)
,有大大知道是什麼原因嗎。
我使用C#的Newtonsoft測試,你的listArray
變成"null"
(null字串)而不是null
(null),所以問題在前一行要先解決,可以對jsonObject.Item("List")
做string.isnullorempty
跟IsNothing
等判斷,視你資料可能的值而定