将JSON响应转换为DataTable [英] Convert JSON response to DataTable

查看:901
本文介绍了将JSON响应转换为DataTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JSON中提供了以下示例响应。我希望能够将其转换为C#DataTable,以便我可以循环遍历元素和程序。

  {
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,
:{
录音:http://link.com
}
},{
type:call,
uri :http://link.com
created:2014-07-22T15:21:02.000 + 0000,
callType:external,
来自:01234567895,
to:01234567898,
callId:ast02-1406042397.63768,
links:{
recordings http://link.com
}
}],
nextPage:http://link.com
}

我正在使用JSON.net。麻烦是当我转到使用以下的数据表:

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

调试器返回..



任何想法任何人?

解决方案

由于 ,建议您需要创建自定义类,并将发布的JSON反序列化,而不是 DataTable 。原因很简单:为了反序列化JSON字符串到 DataTable ,JSON字符串必须遵循一些特定的格式。由于 DataTable 表示一列行和列,您的JSON字符串必须表示集合(JavaScript数组)。 JSON字符串中的所有嵌套对象也必须是JavaScript数组的一部分,因为内置 DataTableConverter (当您尝试将JSON反序列化为<$ c $时使用c> DataTable type)将无法反序列化JSON字符串。您获得的例外是因为以下行:

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

如果将其更改为

 links:[{
录音:http://link.com
}]

您将会更接近于将其正确反序列化为 DataTable



下一步是使整个JSON字符串成为一个JavaScript数组。这样做:

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

},
{
type:call,
uri:http:// link 。
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
}]

之后,您可以将其反序列化为 DataTable ,其中包含以下行:

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

如果更改JSON字符串不是一个选项,则必须将JSON反序列化为自定义类作为 LB 建议。


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

这篇关于将JSON响应转换为DataTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆