我有一个Javascript控件,它将JSON字符串作为AJAX返回给服务器。但是当我尝试保存时,Newtonsoft正在抛出异常
完成反序列化对象后在JSON字符串中找到的附加文本。
我尝试构建一个非常简单的JSON,如下所示抛出此异常
[{
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}, {
"TopicName": "Hello World",
"OrdinalOrder": 0
}]
我正在调用的API是
DataSet dsTopics = JsonConvert.DeserializeObject<DataSet>(strTopicset);
其中strTopicSet包含上面的字符串。当我在http://www.jsonlint.com/中粘贴上面的字符串时,它会显示Valid JSON。
有人可以指导我出错的地方。我确实通过了Newtonsoft上的StackExchange论坛和典型的错误,但无法确定原因。
我正在使用Newtonsoft的9.0.1.19813版本。
答案 0 :(得分:3)
json格式看起来不像数据集格式。
试试这个。
DataTable dsTopics = JsonConvert.DeserializeObject<DataTable>(strTopicset);
或:(我认为这更有用)
public class Topic
{
public string TopicName { get; set; }
public int OrdinalOrder { get; set; }
}
List<Topic> topics = JsonConvert.DeserializeObject<List<Topic>>(strTopicset);
答案 1 :(得分:1)
您的JSON不代表有效的DataSet
。基于您给出的示例,它应该如下所示:
{
"Topics":
[{"TopicName":"Hello World","OrdinalOrder":0},
{"TopicName":"Hello World","OrdinalOrder":0},
{"TopicName":"Hello World","OrdinalOrder":0},
... etc etc more rows ...
{"TopicName":"Hello World","OrdinalOrder":0},
{"TopicName":"Hello World","OrdinalOrder":0}]
}
此JSON代表一个DataSet
,其中包含一个名为“Topics”的DataTable
,其中两列分别名为“TopicName”和“OrdinalOrder”。