Convert JSON response to DataTable

♀尐吖头ヾ 提交于 2019-12-08 12:41:39

问题


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?


回答1:


As L.B 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 L.B suggested.



来源:https://stackoverflow.com/questions/24995875/convert-json-response-to-datatable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!