Issue using Json.Net to parse JSON to a DataSet

时光怂恿深爱的人放手 提交于 2019-12-29 01:56:06

问题


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.


回答1:


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.


来源:https://stackoverflow.com/questions/20894461/issue-using-json-net-to-parse-json-to-a-dataset

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!