Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I've been trying to parse a block of json using json.net (https://dl.dropboxusercontent.com/u/2976553/json) but it fails stating that there is text after the json object. However, if you look at where it throws the exception

    if (checkAdditionalContent)
    {
      if (reader.Read() && reader.TokenType != JsonToken.Comment )
        throw new JsonSerializationException("Additional text found in JSON string after finishing deserializing object.");
    }

I checked the TokenType, and it is an EndObject which doesn't seem like something that should generate an exception. I modified the code to ignore EndOjbect as well, but it doesn't seem to parse anything.

I'm using this..

DataSet ds = JsonConvert.DeserializeObject<DataSet>(response);

I've pasted the json into a number of online checkers and they all report it as valid data.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
172 views
Welcome To Ask or Share your Answers For Others

1 Answer

The reason it doesn't work is because your JSON data doesn't conform to the structure it would need to in order to deserialize into a DataSet. If you take a look at the example from the documentation, the data needs to be structured like this:

{
    "table1" : 
    [
        {
            "column1" : "value1",
            "column2" : "value2"
        },
        {
            "column1" : "value3",
            "column2" : "value4"
        }            
    ],
    "table2" : 
    [
        {
            "column1" : "value1",
            "column2" : "value2"
        },
        {
            "column1" : "value3",
            "column2" : "value4"
        }            
    ]
}

In other words, the outer object contains properties which represent tables. The property names correspond to the names of the tables, and the values are all arrays of objects where each object represents one row in the table. The objects' properties correspond to the column names, and their values are the row data. The row data values must be simple types such as string, int, bool, etc. (Arrays of simple types and nested data tables are also supported if you're using Json.Net 6.0 or later.)

Your JSON data is MUCH more complex than this, and is deeply nested. You will not be able to get Json.Net to deserialize it into a DataSet unless you write your own custom JsonConverter to do it. And I don't think it would be worth the effort to do so.

Instead, I would consider one of these other alternatives:

  1. Create a strongly-typed class hierarchy and deserialize into that. You can use json2csharp.com to help generate your classes. (Note, however, that json2csharp is not foolproof--sometimes you will need to edit the classes it generates to get things to work properly.)
  2. Deserialize into a JObject and use Json.Net's LINQ-to-JSON API to navigate and extract the data you need. Here is an example that might help with that. There are many other examples in the documentation as well as here on StackOverflow.
  3. Deserialize into a dynamic. This approach makes it pretty easy to get at your data, assuming you know its structure well, but you lose intellisense and compile-time checking.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...