2016-08-26 11 views
0

私はC#が新しく、APIからのJSONレスポンスをデシリアライズし、DataTable(そしてSQL Server)への応答を再送信しています。JSONからDataTableへのデシリアライズのトラブル

私はJSON.NETとRestSharpを使用しています。

ここではConvert Json String to C# Object Listの概要を説明し、FastMemberを使用してオブジェクトをデータテーブルに変換しようとしていたが、私は不合理だった。ここで

はJSONレスポンスの例である:

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

エラー:

ここ
{ 
    "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": "[email protected]", 
    "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 
    } 
} 

は、私がここで

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; } 
} 

json2csharp.comから生成されたクラスであることは私のコードです

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

created_atがdatetimeなどであるようないくつかの問題があります。ルートオブジェクトのコードも表示されません。これを試してみてください:

public class Rootobject 
{ 
    public string status { get; set; } 
    public object[] messages { get; set; } 
    public bool success { get; set; } 
    public Data data { get; set; } 
} 

public class Data 
{ 
    public Identifier[] identifiers { get; set; } 
    public Activity[] activities { get; set; } 
    public Transaction[] transactions { get; set; } 
    public object[] devices { get; set; } 
    public 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 Social_Handles social_handles { get; set; } 
    public DateTime 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 Category_Spend[] category_spend { get; set; } 
    public int ticketing_spend { get; set; } 
    public int pos_points { get; set; } 
    public float pos_spend { get; set; } 
} 

public class Social_Handles 
{ 
    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 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 DateTime created_at { get; set; } 
    public string source_url { get; set; } 
    public int worth { get; set; } 
    public int awarded { get; set; } 
} 

public class Transaction 
{ 
    public DateTime purchased_at { get; set; } 
    public DateTime created_at { get; set; } 
    public string data_type { get; set; } 
    public Transaction_Items[] 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 Transaction_Items 
{ 
    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 Category_Spend 
{ 
    public string name { get; set; } 
    public float spend { get; set; } 
} 
+0

私はクラスを更新しましたが、私は速いメンバーを適切に利用していないようです。 私は、次のようなエラーメッセージが出てい: '' FastMember_dynamic で発生した型「System.ArgumentOutOfRangeExceptionが」の未処理の例外私はDataTableのを作成するために、リストにJSONレスポンスを変更する必要がありますか? – ishfuseini

関連する問題