vb.net Newtonsoft.Json.JsonReaderException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18942585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 15:10:28  来源:igfitidea点击:

Newtonsoft.Json.JsonReaderException

jsonvb.netjson.net

提问by user2799017

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:

我对 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"
    }
]

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

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

这是我尝试解析 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?

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

回答by Brian Rogers

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.Parseinstead.

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

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 Eachloop needs to be expecting JObjectitems, not JPropertyitems. Once you have each item, you can then get the properties from them as needed.

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

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.

我不确定你想用这item.CreateReader()条线做什么,因为你没有对它的返回值做任何事情,而且你似乎也不需要。同样,我也对您的说法感到困惑MsgBox(item.Value("img")),因为 JSON 中的任何地方都没有“img”属性。所以这将始终为空。

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.

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