Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
185 views
in Technique[技术] by (71.8m points)

c# - Issue using Json.Net to parse JSON to a DataSet

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

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

1 Reply

0 votes
by (71.8m points)

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...