Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON.NET is throwing 'additional text found in JSON string after finishing deserializing object."

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.

like image 668
Deepak Vasudevan Avatar asked Apr 12 '17 08:04

Deepak Vasudevan


2 Answers

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);
like image 55
levent Avatar answered Oct 02 '22 08:10

levent


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.

like image 24
Equalsk Avatar answered Oct 02 '22 08:10

Equalsk