问题
I thought JSON was supposed to be easy to use.
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.
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)));
,
I get this error:
"Unexpected JSON token while reading DataTable: EndArray".
If I use the text from "response.Content" and validate it here: http://www.jsonlint.com/#, the results says "Valid JSON".
When I use the debugger to view the "response.Content" as Json, it shows the correct records that I am looking for.
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)));
I also tried this:
XmlDocument xd1 = new XmlDocument();
xd1 = (XmlDocument)JsonConvert.DeserializeXmlNode(response.Content, "RootObject");
DataSet dataset = new DataSet();
dataset.ReadXml(new XmlNodeReader(xd1));
which I got from here: http://www.c-sharpcorner.com/blogs/how-to-parse-json-string-to-dataset-in-c-sharp1
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."
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 data
{
"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
}]
}]
}
回答1:
Here's a brief overview of what you need to do.
Taking from NewtonSoft - Deserialize Object example
Snippet of your data
{ "Id": 1, "CustomerGuid": "a940dc03-5f52-47d2-9391-8597b3b31cf2", "UserName": "tony@lakesideos.com", "Email": "tony@lakesideos.com"
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...
}
This is stored in a list of data
"data": [ { ... }, { ... } ]
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": [{
That class starts with
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);
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
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
回答2:
Try starting from Response.Data instead of Response.Content, that will get you one layer in to expose the actual data to deserialize.
For example:
var response = _requestProcessor.Process<Request, Response>(
new Request(id)).dataList;
return new JsonNetResult() { Data = response, JsonRequestBehavior= JsonRequestBehavior.AllowGet };
Whatever the Request object is, it has an object that holds the responses' data in it; that is the actual item to deserialize.
Perhaps what you need to do is what Cricket_007 suggests, and make a class to encapsulate the Success object returned in the JSON, then deserialize to one Success object and a Data object, which is a list of the objects you're trying to get to. The steps you need to follow are:
Convert the whole JSON string to an object that consists of the two objects within, Success and Data.
JSONObject content = new JSONObject(Response.Content);
perhaps, this is untested.From that object, deserialize just the Data object within it.
content.data
-or-content["data"]
or some such should get you to the parts you want to work with.
来源:https://stackoverflow.com/questions/40294206/i-am-trying-to-deserialize-json-data-to-a-dataset