我认为JSON应该很容易使用。
我在Visual Studio 2015中有一个C#.NET Windows窗体程序,用作使用RestSharp作为HTTP客户端的Web API 2远程服务器的客户端。
从API调用返回的数据是Json格式。但是,当我尝试使用以下语句将返回的Json反序列化为数据集时:
DataSet data = (DataSet)JsonConvert.DeserializeObject(response.Content, (typeof(DataSet)));
,
我收到此错误:
“读取DataTable时出现意外的JSON令牌:EndArray”。
如果我使用“response.Content”中的文本并在此处验证:http://www.jsonlint.com/#,则结果显示为“Valid JSON”。
当我使用调试器将“response.Content”视为Json时,它会显示我正在寻找的正确记录。
我尝试过不同格式的JsonConvert,例如:
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'。”
有没有人知道如何使用“易于使用”,“最受欢迎”的JSON格式将我的Json数据反序列化为数据集并提供一个数据表?
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
}]
}]
}
答案 0 :(得分:1)
以下是您需要做的简要概述。
从NewtonSoft - Deserialize Object示例中获取
您的数据片段
{ "Id": 1, "CustomerGuid": "a940dc03-5f52-47d2-9391-8597b3b31cf2", "UserName": "tony@lakesideos.com", "Email": "tony@lakesideos.com"
这是Customer
个对象。它需要它自己的C#模型类。
public class Customer {
int id;
string guid;
string username;
string email;
// etc...
// getters and setters...
}
这存储在data
"data": [ { ... }, { ... } ]
您需要另一个Object来保存这些对象的列表以及布尔值success
。我们称之为Response
。
{ "success": true, "data": [{
该课程以
开头public class Response {
bool success;
IList<Customer> data;
然后,您需要其他列表和对象类,如此
"CustomerRoles": [{ "Id": 3, "Name": "Registered", "SystemName": "Registered" }],
一旦你对整个课程领域进行了建模,你就可以拥有
Response res = JsonConvert.DeserializeObject<Response>(json);
由于您似乎已经尝试过,并且收到有关END_ARRAY字符的错误,因此DataSet
类中的某些内容未正确映射到列表
然后让res.data
访问数据列表,然后你可以遍历那些Customer
对象来构建你的数据集/表对象,我假设你在一些GUI列表中显示你的数据
答案 1 :(得分:0)
尝试从Response.Data而不是Response.Content开始,这将为您提供一层以显示实际数据以进行反序列化。
例如:
var response = _requestProcessor.Process<Request, Response>(
new Request(id)).dataList;
return new JsonNetResult() { Data = response, JsonRequestBehavior= JsonRequestBehavior.AllowGet };
无论Request对象是什么,它都有一个对象,用于保存响应的数据;这是要反序列化的实际项目。
或许您需要做的是Cricket_007建议的内容,并创建一个类来封装JSON中返回的Success对象,然后反序列化为一个Success对象和一个Data对象,这是您正在尝试的对象的列表到达,得到。 您需要遵循的步骤是:
将整个JSON字符串转换为包含Success和Data中的两个对象的对象。
JSONObject content = new JSONObject(Response.Content);
或许,这是未经测试的。
从该对象中,仅反序列化其中的Data对象。
content.data
- 或 - content["data"]
或其他一些应该让你到达你想要使用的部分。