I have a Javascript control that returns JSON string as an AJAX to the server. But when I try to save, Newtonsoft is throwing the exception
Additional text found in JSON string after finishing deserializing object.
I tried building a very simple JSON as below which threw this exception
[{
"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
}]
And the API I am invoking is
DataSet dsTopics = JsonConvert.DeserializeObject<DataSet>(strTopicset);
where strTopicSet contains the above string. And when I paste the above string in http://www.jsonlint.com/ it says Valid JSON.
Can some one guide me where I am going wrong. I did go through StackExchange forums on Newtonsoft and the typical error but could not zero in on the exact cause of this.
I am using 9.0.1.19813 version of Newtonsoft.
json format is not look like dataset format.
try this.
DataTable dsTopics = JsonConvert.DeserializeObject<DataTable>(strTopicset);
OR: (I think this is more useful)
public class Topic
{
public string TopicName { get; set; }
public int OrdinalOrder { get; set; }
}
List<Topic> topics = JsonConvert.DeserializeObject<List<Topic>>(strTopicset);
Your JSON does not represent a valid DataSet
. Based on the example you've given it should look like this:
{
"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}]
}
This JSON would represent a DataSet
containing a single DataTable
named "Topics" with two columns named "TopicName" and "OrdinalOrder" respectively.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With