2016-10-27 9 views
1

JSONが使いやすいと思われました。JSONデータをデータセットに逆シリアル化しようとしています

私はVisual Studio 2015のC#.NET Windowsフォームプログラムを、HTTPクライアントとしてRestSharpを備えたWeb API 2リモートサーバーを使用するクライアントとして使用しています。

API呼び出しから返されるデータはJson形式です。しかし、ときに私はこの文を使用してデータセットに返されたJSONをデシリアライズしよう:

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

私はこのエラーを取得する:

"Unexpected JSON token while reading DataTable: EndArray".

私は「response.Content」からテキストを使用している場合それをここで確認してください:http://www.jsonlint.com/#、結果に「有効なJSON」と表示されます。

私はデバッガを使用して "response.Content"をJsonとして表示すると、私が探している正しいレコードが表示されます。

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

私もこれを試してみました

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

は私のようなJsonConvertの異なるフォーマットを、試してみましたエラーは発生しますが、データセットには4つのテーブル(RootObject、data、CustomerRoles、およびShoppingCartitem)が含まれています。 3つのテーブルを削除しようとしましたが、削除する方法がわからない制約と外部キーがあり、制約を削除するまでテーブルを削除できません。制約を削除しようとすると、「一意制約 'Constraint1'を削除できません。外部キー制約 'RootObject_data'を最初に削除してください。」

「使いやすい」「最も一般的な」JSON形式を使用して、自分のJsonデータをデータセットに逆シリアル化し、1つのデータテーブルを出力する方法を知っている人はいますか?

JSONデータ

{ 
    "success": true, 
    "data": [{ 
     "Id": 6, 
     "CustomerGuid": "70b390d8-82d5-4bba-aa68-fc8268a1b1ff", 
     "UserName": "[email protected]", 
     "Email": "[email protected]", 
     "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": "[email protected]", 
     "Email": "[email protected]", 
     "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": "[email protected]", 
     "Email": "[email protected]", 
     "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": "[email protected]", 
     "Email": "[email protected]", 
     "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": "[email protected]", 
     "Email": "[email protected]", 
     "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": "[email protected]", 
     "Email": "[email protected]", 
     "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

データに「最も一般的な」フォーマットはありません。 "予期しないJSONトークン"は、使用しているパーサーが "EndArray"または ']'文字を期待していなかったことを意味します。 –

+0

JSONデータを貼り付けてください。 – Gusman

+0

@ cricket_007申し訳ありませんが、私がここに行くとhttp://www.newtonsoft.com/jsonは、最も人気のある.NETライブラリであると主張しています。 – Tony

答えて

1

ここにあなたが何をする必要があるかの簡単な概要です。

これはCustomerオブジェクトであるNewtonSoft - Deserialize Object例からデータの

スニペット

{ 
    "Id": 1, 
    "CustomerGuid": "a940dc03-5f52-47d2-9391-8597b3b31cf2", 
    "UserName": "[email protected]", 
    "Email": "[email protected]" 

を取ります。それはそれ自身のC#モデルクラスを必要とします。

public class Customer { 

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

    // getters and setters... 
} 

これは、あなたがそれらのオブジェクトのリストに加えブールsuccess値を保持するために別のオブジェクトを必要とするdata

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

のリストに格納されます。これをResponseとしましょう。あなたはクラスのドメイン全体をモデル化したらクラスはその後

public class Response { 

    bool success; 
    IList<Customer> data; 

で始まる、あなたは、この

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

のようなもののために他のリストとオブジェクトクラスを必要とする

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

できるはずです

Response res = JsonConvert.DeserializeObject<Response>(json); 
あなたが持っているように見えるので、0

が既にことを試みたが、END_ARRAY文字に関するエラーを取得している、あなたのDataSetクラス内の何かが正しくリストにマッピングされていない

そしてあなたに、その後、データのリストにアクセスするにはres.dataを取得これらのオブジェクトをループしてデータセット/テーブルオブジェクトを構築するには、GUIのリストにデータを表示することを前提としています。

+2

http://json2csharp.com/で簡単にあなたのモデルをモデル化することができます。jsonファイルの内容をコピーして貼り付けてください。 – McNets

+0

ありがとう@mcNets私はJavaのようなサイトがあると知っていました –

+0

私は使用していますxmlファイルをデータセットに変換し、xmlをdatasetに変換してforeigenキーをdataset.tablesに追加しますが、独自のデータセット(データセットエディタを使用)を定義してからReadXmlこのデータセットを使用します。 – McNets

0

Response.Contentの代わりにResponse.Dataを試してみてください。実際のデータを公開してデシリアライズします。たとえば
:Requestオブジェクトは、それはそれで応答のデータを保持するオブジェクトを持ち、あるものは何でも

var response = _requestProcessor.Process<Request, Response>(
       new Request(id)).dataList; 

return new JsonNetResult() { Data = response, JsonRequestBehavior= JsonRequestBehavior.AllowGet }; 

。デシリアライズする実際のアイテムです。

おそらく、Cricket_007が示唆していることは、JSONで返されたSuccessオブジェクトをカプセル化し、次に1つのSuccessオブジェクトとDataオブジェクトに逆シリアル化するクラスを作成することですにしようとしている。あなたはに従う必要があり 手順は次のとおりです。

  1. 成功とデータ、内の2つのオブジェクトで構成されたオブジェクトへの全体JSON文字列に変換します。おそらく、これはテストされていません。 JSONObject content = new JSONObject(Response.Content);

  2. そのオブジェクトから、そのオブジェクト内のDataオブジェクトだけを逆シリアル化します。

    content.data - または - content["data"]

またはそのようないくつかのあなたが仕事をしたい部分にあなたを取得する必要があります。

+0

Brinky、Response.Dataはありません。たぶん私はあなたを理解していないが、私は要求オブジェクトに応答のデータがどこにあるのか分からない。ありがとう。 – Tony

+0

Response.Contentオブジェクトは、呼び出しているサービスからの応答全体をカプセル化しています。あなたがしたいことは、応答、コンテンツの部分をデータ内のオブジェクトにさらに細分化し、その中のオブジェクトを分解し、その部分を逆シリアル化します。それはあなたの用途に関連する部分です。おそらくJSONObjectに返されたJSON文字列全体を変換します: – Brinky

+0

Brinky、正しく理解すれば、私はresponse.Content文字列から部分文字列を作成する必要があり、その部分文字列にデータセットに逆シリアル化したいデータが含まれているようです。私は本質的に次のようなJsonデータの部分を削除します: ""成功 ":真"。 – Tony

関連する問題