Newtonsoft.Json.JsonReaderException [英] Newtonsoft.Json.JsonReaderException

查看:850
本文介绍了Newtonsoft.Json.JsonReaderException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a problem with Newtonsoft.Json. I'm trying to parse JSON from a URL but I'm getting an error. Here is the 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"
    }
]

这是我正在使用的代码:

Here is the code I am using:

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时收到的错误:

This is the error I receive when I try to parse the JSON:

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

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

Can you help me solve this?

推荐答案

由于使用JObject.Parse(它需要一个JSON对象,但您的JSON包含一个数组),因此出现此错误.若要更正此问题,请改用JArray.Parse.

You are getting this error because you are using JObject.Parse, which expects a single JSON object, but your JSON contains an array. To correct this, use JArray.Parse instead.

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

But, there is another problem: the rest of your code is not set up to handle the results correctly. Because your results are an array of objects, your For Each loop needs to be expecting JObject items, not JProperty items. Once you have each item, you can then get the properties from them as needed.

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

I am not sure what you were trying to do with the item.CreateReader() line, as you are not doing anything with its return value, and you don't seem to need anyway. Similarly, I am also confused with your MsgBox(item.Value("img")) line, because there is no "img" property anywhere in the JSON. So this will always be null.

这是一些经过更正的代码,它们将解析JSON并显示结果中每个对象的所有属性.这应该为您提供了一个起点.

Here is some corrected code which will parse the JSON and display all the properties for each object in the results. This should give you a starting point to work with.

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

这篇关于Newtonsoft.Json.JsonReaderException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆