我正在尝试将JSON数据反序列化到数据集 [英] I am trying to deserialize JSON data to a dataset

查看:78
本文介绍了我正在尝试将JSON数据反序列化到数据集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为JSON应该易于使用.

I thought JSON was supposed to be easy to use.

我在Visual Studio 2015中有一个C#.NET Windows窗体程序,该程序用作使用Web API 2远程服务器并使用RestSharp作为HTTP客户端的客户端.

I have a C# .NET Windows form program in Visual Studio 2015 being used as a client consuming a Web API 2 remote server with RestSharp as the HTTP client.

API调用返回的数据为Json格式.但是,当我尝试使用以下语句将返回的Json反序列化为数据集时:

the data returned from the API call is in Json format. However, when I try to deserialize the returned Json into a dataset using this statement:

DataSet data = (DataSet)JsonConvert.DeserializeObject(response.Content, (typeof(DataSet)));

我收到此错误:

读取DataTable:EndArray时出现意外的JSON令牌".

"Unexpected JSON token while reading DataTable: EndArray".

如果我使用"response.Content"中的文本并在此处进行验证: http://www.jsonlint. com/#,结果显示为有效JSON".

If I use the text from "response.Content" and validate it here: http://www.jsonlint.com/#, the results says "Valid JSON".

当我使用调试器以Json身份查看"response.Content"时,它将显示我正在寻找的正确记录.

When I use the debugger to view the "response.Content" as Json, it shows the correct records that I am looking for.

我尝试了不同格式的JsonConvert,例如:

I have tried different formats of JsonConvert, such as:

DataSet data = JsonConvert.DeserializeObject<DataSet>(response.Content);
DataTable datat = (DataTable)JsonConvert.DeserializeObject(response.Content, (typeof(DataTable)));

我也尝试过:

XmlDocument xd1 = new XmlDocument();
xd1 = (XmlDocument)JsonConvert.DeserializeXmlNode(response.Content, "RootObject");
DataSet dataset = new DataSet();
dataset.ReadXml(new XmlNodeReader(xd1));

我从这里得到的

: http://www.c-sharpcorner.com/blogs/how-to-parse-json-string-to-dataset-in-c-sharp1

这似乎没有给出错误,但数据集包含四个表(RootObject,data,CustomerRoles和ShoppingCartitems). 我尝试删除了三个表,但是有些约束和外键我不知道如何删除,除非删除约束,否则不允许我删除表.如果我尝试删除约束,则会出现此错误:无法删除唯一约束'Constraint1'.首先删除外键约束'RootObject_data'."

That seems to work without giving an error, but the dataset contains four tables ( RootObject, data, CustomerRoles and ShoppingCartitems). I tried removing three of the tables, but there are constraints and foreignkeys that I don't know how to remove and it won't allow me to remove the tables until I remove the constraints. If I try to remove the constraints, it gives this error: "Cannot remove unique constraint 'Constraint1'. Remove foreign key constraint 'RootObject_data' first."

有人知道如何使用易于使用",最受欢迎"的JSON格式将我的Json数据反序列化为数据集并得出一个数据表吗?

Does anybody know how to use the "easy to use", "most popular" JSON format to deserialize my Json data into a dataset and come out with one table of data?

JSON数据

{
    "success": true,
    "data": [{
        "Id": 6,
        "CustomerGuid": "70b390d8-82d5-4bba-aa68-fc8268a1b1ff",
        "UserName": "victoria_victoria@nopCommerce.com",
        "Email": "victoria_victoria@nopCommerce.com",
        "CustomerRoles": [{
            "Id": 3,
            "Name": "Registered",
            "SystemName": "Registered"
        }],
        "AdminComment": null,
        "IsTaxExempt": false,
        "AffiliateId": 0,
        "VendorId": 0,
        "HasShoppingCartItems": false,
        "Active": false,
        "Deleted": false,
        "IsSystemAccount": false,
        "SystemName": null,
        "LastIpAddress": null,
        "CreatedOnUtc": "\/Date(1472933472393)\/",
        "LastLoginDateUtc": null,
        "LastActivityDateUtc": "\/Date(1472933472393)\/",
        "ExternalAuthenticationRecords": [],
        "ShoppingCartItems": []
    }, {
        "Id": 5,
        "CustomerGuid": "eb9e6f24-f362-4c10-942a-366e2919dc11",
        "UserName": "brenda_lindgren@nopCommerce.com",
        "Email": "brenda_lindgren@nopCommerce.com",
        "CustomerRoles": [{
            "Id": 3,
            "Name": "Registered",
            "SystemName": "Registered"
        }],
        "AdminComment": null,
        "IsTaxExempt": false,
        "AffiliateId": 0,
        "VendorId": 0,
        "HasShoppingCartItems": false,
        "Active": false,
        "Deleted": false,
        "IsSystemAccount": false,
        "SystemName": null,
        "LastIpAddress": null,
        "CreatedOnUtc": "\/Date(1472933472363)\/",
        "LastLoginDateUtc": null,
        "LastActivityDateUtc": "\/Date(1472933472363)\/",
        "ExternalAuthenticationRecords": [],
        "ShoppingCartItems": []
    }, {
        "Id": 4,
        "CustomerGuid": "9f46dbae-6942-410c-90b8-9b38a0890064",
        "UserName": "james_pan@nopCommerce.com",
        "Email": "james_pan@nopCommerce.com",
        "CustomerRoles": [{
            "Id": 3,
            "Name": "Registered",
            "SystemName": "Registered"
        }],
        "AdminComment": null,
        "IsTaxExempt": false,
        "AffiliateId": 0,
        "VendorId": 0,
        "HasShoppingCartItems": false,
        "Active": false,
        "Deleted": false,
        "IsSystemAccount": false,
        "SystemName": null,
        "LastIpAddress": null,
        "CreatedOnUtc": "\/Date(1472933472317)\/",
        "LastLoginDateUtc": null,
        "LastActivityDateUtc": "\/Date(1472933472317)\/",
        "ExternalAuthenticationRecords": [],
        "ShoppingCartItems": []
    }, {
        "Id": 3,
        "CustomerGuid": "6277386b-13ee-427b-9cfe-4ebfa487c340",
        "UserName": "arthur_holmes@nopCommerce.com",
        "Email": "arthur_holmes@nopCommerce.com",
        "CustomerRoles": [{
            "Id": 3,
            "Name": "Registered",
            "SystemName": "Registered"
        }],
        "AdminComment": null,
        "IsTaxExempt": false,
        "AffiliateId": 0,
        "VendorId": 0,
        "HasShoppingCartItems": false,
        "Active": false,
        "Deleted": false,
        "IsSystemAccount": false,
        "SystemName": null,
        "LastIpAddress": null,
        "CreatedOnUtc": "\/Date(1472933472253)\/",
        "LastLoginDateUtc": null,
        "LastActivityDateUtc": "\/Date(1472933472253)\/",
        "ExternalAuthenticationRecords": [],
        "ShoppingCartItems": []
    }, {
        "Id": 2,
        "CustomerGuid": "241f45f1-b38c-4e22-8c5a-743fa3276620",
        "UserName": "steve_gates@nopCommerce.com",
        "Email": "steve_gates@nopCommerce.com",
        "CustomerRoles": [{
            "Id": 3,
            "Name": "Registered",
            "SystemName": "Registered"
        }],
        "AdminComment": null,
        "IsTaxExempt": false,
        "AffiliateId": 0,
        "VendorId": 0,
        "HasShoppingCartItems": false,
        "Active": false,
        "Deleted": false,
        "IsSystemAccount": false,
        "SystemName": null,
        "LastIpAddress": null,
        "CreatedOnUtc": "\/Date(1472933472207)\/",
        "LastLoginDateUtc": null,
        "LastActivityDateUtc": "\/Date(1472933472207)\/",
        "ExternalAuthenticationRecords": [],
        "ShoppingCartItems": []
    }, {
        "Id": 1,
        "CustomerGuid": "a940dc03-5f52-47d2-9391-8597b3b31cf2",
        "UserName": "tony@lakesideos.com",
        "Email": "tony@lakesideos.com",
        "CustomerRoles": [{
            "Id": 1,
            "Name": "Administrators",
            "SystemName": "Administrators"
        }, {
            "Id": 2,
            "Name": "Forum Moderators",
            "SystemName": "ForumModerators"
        }, {
            "Id": 3,
            "Name": "Registered",
            "SystemName": "Registered"
        }],
        "AdminComment": null,
        "IsTaxExempt": false,
        "AffiliateId": 0,
        "VendorId": 0,
        "HasShoppingCartItems": true,
        "Active": true,
        "Deleted": false,
        "IsSystemAccount": false,
        "SystemName": null,
        "LastIpAddress": "71.185.255.7",
        "CreatedOnUtc": "\/Date(1472933470783)\/",
        "LastLoginDateUtc": "\/Date(1477522483903)\/",
        "LastActivityDateUtc": "\/Date(1477523996553)\/",
        "ExternalAuthenticationRecords": [],
        "ShoppingCartItems": [{
            "Id": 1,
            "StoreId": 1,
            "ShoppingCartTypeId": 1,
            "CustomerId": 1,
            "ProductId": 18,
            "AttributesXml": null,
            "CustomerEnteredPrice": 0.0000,
            "Quantity": 1,
            "CreatedOnUtc": "\/Date(1473801903447)\/",
            "UpdatedOnUtc": "\/Date(1473803336207)\/",
            "IsFreeShipping": false,
            "IsShipEnabled": true,
            "AdditionalShippingCharge": 0.0000,
            "IsTaxExempt": false
        }]
    }]
}

推荐答案

以下是您需要做的简短概述.

Here's a brief overview of what you need to do.

取自 NewtonSoft-反序列化对象示例

您的数据片段

{
    "Id": 1,
    "CustomerGuid": "a940dc03-5f52-47d2-9391-8597b3b31cf2",
    "UserName": "tony@lakesideos.com",
    "Email": "tony@lakesideos.com"

这是一个Customer对象.它需要自己的C#模型类.

This is a Customer object. It needs it's own C# model class.

public class Customer {

    int id;
    string guid;
    string username;
    string email;
    // etc...

    // getters and setters...
}

此内容存储在data

"data": [ { ... }, { ... } ]

您需要另一个Object来保存这些对象的列表以及布尔success值.我们将此称为Response.

You need another Object to hold a list of those objects plus a boolean success value. Let's call this one Response.

{
    "success": true,
    "data": [{

该课程始于

public class Response {

    bool success;
    IList<Customer> data;

然后,您需要类似这样的其他列表和对象类

Then, you need other lists and object classes for something like this

"CustomerRoles": [{
        "Id": 3,
        "Name": "Registered",
        "SystemName": "Registered"
    }],

一旦您对整个类域进行了建模,您就应该能够拥有

Once you've modeled the entire domain of classes, you should be able to have

Response res = JsonConvert.DeserializeObject<Response>(json);

由于您似乎已经尝试过该方法,并且遇到了有关END_ARRAY字符的错误,因此DataSet类中的某些内容未正确映射到列表

Since you seem to have already tried that, and are getting an error about the END_ARRAY character, something within your DataSet class is not correctly mapped to a List

然后获取res.data来访问数据列表,然后您可以遍历那些Customer对象以构建数据集/表对象,我假设将您的数据显示在GUI的某些列表中

And then get res.data to access the list of data, then you can loop over those Customer objects to build your Dataset / table objects, I assume to display your data in some list of the GUI

这篇关于我正在尝试将JSON数据反序列化到数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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