Newtonsoft.Json.JsonReaderException

用户名

我对Newtonsoft.Json有问题。我正在尝试从URL解析JSON,但出现错误。这是JSON:

[
    {
        "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.

你能帮我解决这个问题吗?

布莱恩·罗杰斯(Brian Rogers)

您收到此错误的原因是您正在使用JObject.Parse,它需要一个JSON对象,但是您的JSON包含一个数组。要更正此问题,请JArray.Parse改用。

但是,还有另一个问题:您的其余代码未设置为正确处理结果。由于结果是对象数组,因此For Each循环需要包含JObject项目,而不是JProperty项目。拥有每个项目后,便可以根据需要从它们中获取属性。

我不确定您要使用该item.CreateReader()行做什么,因为您没有使用它的返回值做任何事情,而且您似乎也不需要。同样,我也对您的代码感到困惑MsgBox(item.Value("img")),因为JSON中的任何地方都没有“ img”属性。因此,它将始终为null。

这是一些经过更正的代码,它们将解析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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Xamarin.Android:`Newtonsoft.Json.JsonReaderException`

Newtonsoft.Json v11 - DeserializeObject 抛出 JsonReaderException

“Newtonsoft.Json.JsonReaderException”类型的未处理异常

Newtonsoft.Json.JsonReaderException: '解析值时遇到意外字符

Newtonsoft.Json.JsonReaderException:阅读完JSON内容后遇到的其他文本:

Newtonsoft.Json.JsonReaderException:無效的 JavaScript 屬性標識符字符:,

Newtonsoft.Json.JsonReaderException 輸入字符串 '0.64' 不是有效整數

在本地运行Azure函数(V2)时引发Newtonsoft.Json.JsonReaderException

Newtonsoft.Json.JsonReaderException:无法将字符串转换为DateTime:

将部分json转换为c#对象时发生类型为'Newtonsoft.Json.JsonReaderException'的第一次机会异常

NewtonSoft Json转换列表

构建Json Newtonsoft Jarray

Newtonsoft Json 动态对象

在Json.Net中从JsonReaderException查找无效值

在命名空间中找不到System.Text.Json.JsonReaderException

与Newtonsoft.Json的装配冲突

C#JSON Newtonsoft转换

在Newtonsoft JSON中转义引号

Newtonsoft JSON:TypeNameHandling - $type 目的

用 NewtonSoft 替换 Json 属性

JsonReaderException 试图解析包含 [C#] 中的多个 JSON 对象的 JSON 文件

使用Newtonsoft Json获得json属性类型

无效的JSON对象错误:NewtonSoft Json

Newtonsoft.json JSON.NET

Newtonsoft JSON - 不同的 JSON 结构,对象与数组

使用Newtonsoft.Json生成JSON问题

Newtonsoft json access array inside json

使用 NewtonSoft JSON 解析 JSON 数组

计算 json 对象 newtonsoft json c#