简体   繁体   中英

Need to convert JSON to datatable

I have incoming JSON formatted like this:

{
    "users": [
        {
            "radio_id": "123582",
            "callsign": "ABCD",
            "name": "First Last",
            "city": "Dortmund",
            "state": "Nordrhein-Westfalen",
            "country": "Germany",
            "home_rptr": "W2VL",
            "remarks": "None"
        },
        {
            "radio_id": "789456",
            "callsign": "EFG",
            "name": "Name Here",
            "city": "Dortmund",
            "state": "Nordrhein-Westfalen",
            "country": "Germany",
            "home_rptr": "W2VL",
            "remarks": "None"
        }
    ]
}

It is coming from a web request that I catch into a string called dataReceived. I then use this line of code to convert to a datatable.

DataTable dtData = (DataTable)JsonConvert.DeserializeObject(dataReceived, (typeof(DataTable)));

I'm getting an error of: Unexpected JSON token when reading DataTable. Expected StartArray, got StartObject. Path '', line 1, position 1.

I suspect my problem is the data is in an array, but I'm not sure how to solve this. My objective is to have a table with each row one of the "users" objects in the json.

Can anyone push me in the right direction?

var dt = JObject.Parse(json)["users"].ToObject<DataTable>();

就这些。

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