使用Json.Net问题解析JSON到DataSet [英] Issue using Json.Net to parse JSON to a DataSet

查看:1957
本文介绍了使用Json.Net问题解析JSON到DataSet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图解析使用json.net一个JSON块( https://开头dl.dropboxusercontent.com/u/2976553/json ),但它没有说明是有JSON对象之后的文本。但是,如果你看它抛出异常

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.");
    }



我查了TokenType,它是一个 EndObject 这似乎并不喜欢的事,应该产生一个异常。我修改了代码忽略 EndOjbect 为好,但它似乎没有任何解析

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.

我米使用这个..

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



我已经粘贴了JSON成若干在线跳棋,他们都报告为有效数据。

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

推荐答案

它不工作的原因是因为你的JSON数据不符合结构就需要以反序列化到数据集。如果你看看在例如从文档,数据需要像这样的结构:

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"
        }            
    ]
}

在换句话说,外对象包含代表表格属性。属性名对应表的名称,和值,其中每个对象代表表中的一行对象的所有阵列。该对象的属性对应的列名,其值是该行的数据。行的数据值必须是简单的类型,如字符串,整数,布尔等(简单类型和嵌套的数据表数组,如果你正在使用Json.Net 6.0或更高版本也支持。)

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.)

您的JSON数据比这要复杂得多,而且是深度嵌套。你将无法得到Json.Net将其反序列化到DataSet,除非你编写自己的定制JsonConverter做到这一点。 。我不认为这是值得的,这样做

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. 创建一个强类型的类层次结构和反序列化到该。您可以使用 json2csharp.com 以帮助生成你的类。 (请注意,但是,json2csharp并非万无一失 - 有时你需要编辑它生成得到的东西正常工作类)

  2. 反序列化到一个 JObject 和使用Json.Net的 LINQ到-JSON API 浏览和提取所需的数据。这里是 ,一个例子可能与帮助。有文档中许多其他的例子还有这里的计算器。

  3. 反序列化到一个动态。这种方法使得它很容易让你的数据,假设你知道它的结构很好,但你失去了智能感知和编译时检查。

  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.

这篇关于使用Json.Net问题解析JSON到DataSet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆