简体   繁体   中英

VB.NET - convert JSON data into array

I have a JSON result that I am trying to convert into an array using Newtonsoft.Json . My JSON result i get from the website is along the lines of (formated for readability):

{
"headers":  
    [
        "Shift Date",
        "Shift Number"
    ],
"values":
    [
        ["2016-06-19T00:00:00",0],
        ["2016-06-19T00:00:00",2],
        ["2016-06-19T00:00:00",1]
    ]
}

Code examples I have found say that i should be able to use

Dim arr As JArray = JArray.Parse(response.Content)

This results in an error though with:

An unhandled exception of type 'Newtonsoft.Json.JsonReaderException' occurred in Newtonsoft.Json.dll

Additional information: Error reading JArray from JsonReader. Current JsonReader item is not an array: StartObject. Path '', line 1, position 1.

Any guidance on what could be causing the issue? I suspect it is something with the "headers" but unable to find online any suggestions on how to resolve

You are using JArray but your input is actually an object - ie the data is

{..stuff..}

rather than

[ {..stuff..} ] .

If you restructure your input to be:

[{
    "headers": [
        "Shift Date",
        "Shift Number"
    ],
    "values": [
        ["2016-06-19T00:00:00", 0],
        ["2016-06-19T00:00:00", 2],
        ["2016-06-19T00:00:00", 1]
    ]
}]

You can then use the JArray.Parse(strJson) method.

In the current structure, you should use JObject.Parse .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM