我是C#的新手,我正在从API反序列化JSON响应并将响应重新发送到DataTable(然后重新发送到SQL Server)。
我正在使用JSON.NET和RestSharp。
我按照这里列出的答案Convert Json String to C# Object List并尝试使用FastMember将对象转换为数据表,但我没有成功。
以下是JSON响应的示例:
{
"status": "success",
"messages": [],
"success": true,
"data": {
"identifiers": [
{
"identifier": "123",
"type": "cust_id",
"active": true
},
{
"identifier": "456",
"type": "pid",
"active": true
}
],
"activities": [
{
"identity": "transaction_6408496",
"type": "bypass",
"subtype": "pos",
"subject": "made a purchase",
"created_at": "2016-03-29T20:04:03-05:00",
"source_url": "",
"worth": 26,
"awarded": 26
}
],
"transactions": [
{
"purchased_at": "2016-03-29T20:04:03-05:00",
"created_at": "2016-03-29T20:05:21-05:00",
"data_type": "pos",
"transaction_items": [
{
"total_cents": 525,
"price_cents": 525,
"quantity": 1,
"name": "Soda- Regular Fountain Soda, 24oz",
"category": "NA Bev",
"bucket": "Concessions"
}
],
"transaction_number": "25401170",
"transaction_id": "25401170",
"event_id": "",
"location_id": "3163",
"terminal_id": "34d8cd0fa430e17a",
"member_id": "789",
"table_number": ""
},
{
"purchased_at": "2015-03-21T13:18:20-05:00",
"created_at": "2015-03-23T02:14:46-05:00",
"data_type": "pos",
"transaction_items": [
{
"total_cents": 3315,
"price_cents": 3315,
"quantity": 1,
"name": "YTH SMU FZ HOODIE 2-4T",
"category": "KIDS",
"bucket": "Merchandise"
}
],
"transaction_number": "",
"transaction_id": "01-1047019",
"event_id": "",
"location_id": "0102",
"terminal_id": "",
"member_id": "789",
"table_number": ""
}
],
"devices": [],
"visits": [],
"email": "me@ishfuseini.com",
"email_deliverable": true,
"first_name": "Ish",
"last_name": "Fuseini",
"fanfluence": "",
"profile_url": "http://ishfuseini.com",
"gender": "",
"age": "",
"relationship_status": "",
"religion": "",
"political": "",
"location": "",
"address": "",
"city": "",
"state": "",
"zip": "",
"birthdate": "",
"phone": "",
"social_handles": {
"twitter": "",
"foursquare": "",
"facebook": "",
"instagram": "",
"tvtag": "",
"shopify": "",
"pinterest": "",
"tumblr": ""
},
"created_at": "2014-09-26T17:10:37-05:00",
"tc_accepted_at": "",
"points_available": 26,
"points_spent": 0,
"total_points_earned": 26,
"social_points": 0,
"ticketing_points": 0,
"membership_assignment": false,
"category_spend": [
{
"name": "Concessions",
"spend": 5.25
},
{
"name": "Merchandise",
"spend": 33.15
}
],
"ticketing_spend": 0,
"pos_points": 26,
"pos_spend": 38.4
}
}
以下是我从json2csharp.com
生成的课程public class Identifier
{
public string identifier { get; set; }
public string type { get; set; }
public bool active { get; set; }
}
public class Activity
{
public string identity { get; set; }
public string type { get; set; }
public string subtype { get; set; }
public string subject { get; set; }
public string created_at { get; set; }
public string source_url { get; set; }
public int worth { get; set; }
public int awarded { get; set; }
}
public class TransactionItem
{
public int total_cents { get; set; }
public int price_cents { get; set; }
public int quantity { get; set; }
public string name { get; set; }
public string category { get; set; }
public string bucket { get; set; }
}
public class Transaction
{
public string purchased_at { get; set; }
public string created_at { get; set; }
public string data_type { get; set; }
public List<TransactionItem> transaction_items { get; set; }
public string transaction_number { get; set; }
public string transaction_id { get; set; }
public string event_id { get; set; }
public string location_id { get; set; }
public string terminal_id { get; set; }
public string member_id { get; set; }
public string table_number { get; set; }
}
public class SocialHandles
{
public string twitter { get; set; }
public string foursquare { get; set; }
public string facebook { get; set; }
public string instagram { get; set; }
public string tvtag { get; set; }
public string shopify { get; set; }
public string pinterest { get; set; }
public string tumblr { get; set; }
}
public class CategorySpend
{
public string name { get; set; }
public double spend { get; set; }
}
public class Data
{
public List<Identifier> identifiers { get; set; }
public List<Activity> activities { get; set; }
public List<Transaction> transactions { get; set; }
public List<object> devices { get; set; }
public List<object> visits { get; set; }
public string email { get; set; }
public bool email_deliverable { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
public string fanfluence { get; set; }
public string profile_url { get; set; }
public string gender { get; set; }
public string age { get; set; }
public string relationship_status { get; set; }
public string religion { get; set; }
public string political { get; set; }
public string location { get; set; }
public string address { get; set; }
public string city { get; set; }
public string state { get; set; }
public string zip { get; set; }
public string birthdate { get; set; }
public string phone { get; set; }
public SocialHandles social_handles { get; set; }
public string created_at { get; set; }
public string tc_accepted_at { get; set; }
public int points_available { get; set; }
public int points_spent { get; set; }
public int total_points_earned { get; set; }
public int social_points { get; set; }
public int ticketing_points { get; set; }
public bool membership_assignment { get; set; }
public List<CategorySpend> category_spend { get; set; }
public int ticketing_spend { get; set; }
public int pos_points { get; set; }
public double pos_spend { get; set; }
}
public class RootObject
{
public string status { get; set; }
public List<object> messages { get; set; }
public bool success { get; set; }
public Data data { get; set; }
}
这是我的代码:
IRestResponse response = client.Execute(request);
var content = response.Content;
//Deserialziing Response to Object
var datafromjson = JsonConvert.DeserializeObject<ActivityModels.RootObject>(content);
DataTable table = new DataTable();
using (var reader = ObjectReader.Create(datafromjson.data.ToString()))
{
table.Load(reader);
}
错误:
System.ArgumentOutOfRangeException was unhandled
HResult=-2146233086
Message=Specified argument was out of the range of valid values.
Parameter name: name
ParamName=name
Source=FastMember_dynamic
StackTrace:
at FastMember_dynamic.Char_1.get_Item(Object , String )
at FastMember.ObjectReader.System.Data.IDataRecord.GetValues(Object[] values) in c:\Dev\fast-member\FastMember\ObjectReader.cs:line 300
at System.Data.ProviderBase.DataReaderContainer.CommonLanguageSubsetDataReader.GetValues(Object[] values)
at System.Data.ProviderBase.SchemaMapping.LoadDataRow()
at System.Data.Common.DataAdapter.FillLoadDataRow(SchemaMapping mapping)
at System.Data.Common.DataAdapter.FillFromReader(DataSet dataset, DataTable datatable, String srcTable, DataReaderContainer dataReader, Int32 startRecord, Int32 maxRecords, DataColumn parentChapterColumn, Object parentChapterValue)
at System.Data.Common.DataAdapter.Fill(DataTable[] dataTables, IDataReader dataReader, Int32 startRecord, Int32 maxRecords)
at System.Data.Common.LoadAdapter.FillFromReader(DataTable[] dataTables, IDataReader dataReader, Int32 startRecord, Int32 maxRecords)
at System.Data.DataTable.Load(IDataReader reader, LoadOption loadOption, FillErrorEventHandler errorHandler)
at System.Data.DataTable.Load(IDataReader reader)
at FanMaker_API.GetActivity..ctor() in C:\Users\Ismail Fuseini\Documents\projects\FanMaker_API\FanMaker_API\GetActivity.cs:line 67
at FanMaker_API.Program.Main(String[] args) in C:\Users\Ismail Fuseini\Documents\projects\FanMaker_API\FanMaker_API\Program.cs:line 22
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
我认为JSON的结构正是让我失望的原因。我试图将数据类的数据类型更改为列表,但是我会收到一个错误,要求我更改JSON或更改我将对象反序列化的对象类型。
答案 0 :(得分:0)
您的课程中存在一些问题,例如created_at必须是日期时间等。也无法查看根对象的代码。试试这个:
public void updateWeatherMeasurement(String type, Measurement measurement, FactoryWeather factory) {
WeatherMeasurementEnum m = WeatherMeasurementEnum(type.toUpperCase());
if(!m.isValid(measurement.getAverage())
throw new AppException("Invalid measurement!");
switch(m) {
case TEMPERATURE: factory.setTemperature(measurement);break;
case HUMIDITY: factory.setHumidity(measurement);break;
...
}
}