简体   繁体   中英

Convert JSON response to DataTable

I have the following example response provided in JSON. I'd like to be able to convert this to a C# DataTable so I can loop through the elements and program on from there.

{
  "totalItems" : 10,
  "pageSize" : 20,
  "page" : 1,
  "items" : [ {
    "type" : "call",
    "uri" : "http://link.com",
    "created" : "2014-07-28T10:02:48.000+0000",
    "callType" : "external",
    "from" : "01234567891",
    "to" : "01234567892",
    "callId" : "ast01-1406541749.604847",
    "links" : {
      "recordings" : "http://link.com"
    }
  }, {
    "type" : "call",
    "uri" : "http://link.com"
    "created" : "2014-07-22T15:21:02.000+0000",
    "callType" : "external",
    "from" : "01234567895",
    "to" : "01234567898",
    "callId" : "ast02-1406042397.63768",
    "links" : {
      "recordings" : "http://link.com"
    }
  } ],
  "nextPage" : "http://link.com"
}

I am using JSON.net. Trouble is when I go to convert to DataTable using the following:

DataTable items = JsonConvert.DeserializeObject<DataTable>(jsonString);

the debugger returns..

"Unexpected JSON token while reading DataTable: StartObject"

Any ideas anyone?

As LB suggested you'll need to create custom classes and deserialize the posted JSON into them instead of DataTable . The reason is simple: To deserialize JSON string into DataTable the JSON string must follow some particular format. As DataTable represents a collection of rows and columns your JSON string must represent a collection (javascript array). All nested objects in the JSON string also must be a part of a javascript array, because the built in DataTableConverter (that is used when you try to deserialize JSON to DataTable type) will not be able to deserialize the JSON string. The exception you get is because of the following row:

"links" : {
      "recordings" : "http://link.com"
    }

If you change it to

"links": [{
                "recordings": "http://link.com"
            }]

you'll be one step closer to deserialize it correctly to DataTable .

Next step is to make the whole JSON string a javascript array. Something like this:

[{
    "totalItems": 10,
    "pageSize": 20,
    "page": 1,
    "items": [
        {
            "type": "call",
            "uri": "http://link.com",
            "created": "2014-07-28T10:02:48.000+0000",
            "callType": "external",
            "from": "01234567891",
            "to": "01234567892",
            "callId": "ast01-1406541749.604847",
            "links": [{
                "recordings": "http://link.com"
            }]

        },
        {
            "type": "call",
            "uri": "http://link.com",
            "created": "2014-07-22T15:21:02.000+0000",
            "callType": "external",
            "from": "01234567895",
            "to": "01234567898",
            "callId": "ast02-1406042397.63768",
            "links": [{
                "recordings": "http://link.com"
            }]
        }
    ],
    "nextPage": "http://link.com"
}]

After this you'll be able to deserialize it to DataTable with the following line:

DataTable items = JsonConvert.DeserializeObject<DataTable>(jsonString);

If changing the JSON string is not an option, you'll have to deserialize your JSON to custom classes as LB suggested.

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