2016-12-07 7 views
0

JSON形式の出力を提供するAPIを使用していますが、それに含まれるフィールドについては確信しています。私はそれをリスト形式にデシリアライズしたい 私はNewtonsoft.Json名前空間を使いましたが、あまり役に立たなかった。次の記事は良かったが、キー/値のペアを認識していないので、私の目的には役立たなかった。 記事:http://www.newtonsoft.com/json/help/html/deserializeobject.htmJSON出力のストリームの逆シリアル化

マイコード:

static void GetShares() 
{ 
    WebRequest request = WebRequest.Create("https://shares.ppe.datatransfer.microsoft.com/api/v1/data/shares/"); 

    request.Method = "GET"; 
    request.Headers.Add("Authorization","Basic "+ 
    Convert.ToBase64String(
    Encoding.ASCII.GetBytes("useridandpassword"))); 
    request.ContentType = "application/json"; 
    WebResponse response = request.GetResponse(); 
    Stream dataStream = response.GetResponseStream(); 

    // Open the stream using a StreamReader for easy access. 
    StreamReader reader = new StreamReader(dataStream); 

    // Read the content. 
    string responseFromServer = reader.ReadToEnd(); 

    Console.WriteLine(responseFromServer); 
} 
+2

* "これを読み込み可能な形式にデシリアライズしたい" * - JSONは文字列であるため、人間が判読可能な形式になっています。あなたの質問を編集して、希望の出力形式のサンプルを表示してください。あなたはどの言語を使用しようとしていますか、JavaScriptかC#? – nnnnnn

+0

読み取り可能な形式とは、リストのfromで、つまりある列のすべてのキーと、他方の列の対応する値を意味します。 – dotnetman

+0

@dotnetman投稿を編集するときにタイトルに「編集1」を入れる必要はありません。各投稿には[編集履歴](http://stackoverflow.com/posts/41009904/revisions)があり、編集された内容と変更された内容を示しています。誰でも投稿の編集履歴を見ることができます。 –

答えて

0

これはそれを行う必要があります。

WebRequest request = WebRequest.Create("https://shares.ppe.datatransfer.microsoft.com/api/v1/data/shares/"); 

request.Method = "GET"; 
request.Headers.Add("Authorization", "Basic " + 
Convert.ToBase64String(
Encoding.ASCII.GetBytes("userid:password"))); 
request.ContentType = "application/json"; 
WebResponse response = request.GetResponse(); 
Stream dataStream = response.GetResponseStream(); 

// Open the stream using a StreamReader for easy access. 
StreamReader reader = new StreamReader(dataStream); 

// Read the content. 
string responseFromServer = reader.ReadToEnd(); 

JArray items = JArray.Parse(responseFromServer); 

Console.WriteLine($"{"Keys".PadRight(24)}Values"); 
Console.WriteLine($"{"".PadRight(50, '-')}"); 

foreach (JToken token in items) 
{ 
    Dictionary<string, string> dictionary = token.ToObject<Dictionary<string, string>>(); 

    int length = 28; 

    foreach (var property in dictionary) 
    { 
     Console.WriteLine($"{property.Key.PadRight(length)}{property.Value}"); 
    } 

    Console.WriteLine($"----------------------"); 
} 

これはJArrayとしてペイロードを解析するには、最初の項目(最初nullをチェック)、deserialisesをつかみますそれを辞書に入れてキーを選択します。私はそれが最長の鍵長であるので24を選んだ。 this codeを使用してパディング変数を作成できます。

0

あなたのコードを実行しましたが、次のデータをコンソールに戻して印刷しているようです(読み込み可能な形式ではない)。

[ 
    { 
    "browse_count": 0, 
    "bytes_free_string": null, 
    "bytes_total_string": null, 
    "bytes_unknown": true, 
    "comment_added_at": null, 
    "content_added_at": null, 
    "created_at": "2016-06-09T17:23:53-07:00", 
    "directory": "/", 
    "error_bytes_free_size": null, 
    "error_percent_free": null, 
    "home_share": false, 
    "id": 63, 
    "name": "linuxServerRoot", 
    "node_id": 5, 
    "percent_free": null, 
    "status": null, 
    "status_at": "2016-12-06T21:24:26-08:00", 
    "status_message": null, 
    "updated_at": "2016-12-06T21:24:26-08:00", 
    "warn_bytes_free_size": null, 
    "warn_percent_free": null 
    }, 
    { 
    "browse_count": 6, 
    "bytes_free_string": null, 
    "bytes_total_string": null, 
    "bytes_unknown": true, 
    "comment_added_at": null, 
    "content_added_at": null, 
    "created_at": "2016-10-17T14:52:06-07:00", 
    "directory": "/Windows_RTM", 
    "error_bytes_free_size": null, 
    "error_percent_free": null, 
    "home_share": false, 
    "id": 584, 
    "name": "Windows_RTM1.1", 
    "node_id": 7, 
    "percent_free": null, 
    "status": null, 
    "status_at": "2016-12-06T21:24:33-08:00", 
    "status_message": null, 
    "updated_at": "2016-12-06T21:24:33-08:00", 
    "warn_bytes_free_size": null, 
    "warn_percent_free": null 
    }, 
    { 
    "browse_count": 3, 
    "bytes_free_string": null, 
    "bytes_total_string": null, 
    "bytes_unknown": true, 
    "comment_added_at": null, 
    "content_added_at": null, 
    "created_at": "2016-11-29T00:40:20-08:00", 
    "directory": "/Stats_subscription-For-Demo-Do not delete/WUS-Stats_subscription--Default no stats", 
    "error_bytes_free_size": null, 
    "error_percent_free": null, 
    "home_share": false, 
    "id": 1401, 
    "name": "WUS-Stats_subscription--Default no stats", 
    "node_id": 5, 
    "percent_free": null, 
    "status": null, 
    "status_at": "2016-12-06T21:24:27-08:00", 
    "status_message": null, 
    "updated_at": "2016-12-06T21:24:27-08:00", 
    "warn_bytes_free_size": null, 
    "warn_percent_free": null 
    }, 
    { 
    "browse_count": 0, 
    "bytes_free_string": null, 
    "bytes_total_string": null, 
    "bytes_unknown": true, 
    "comment_added_at": null, 
    "content_added_at": null, 
    "created_at": "2016-11-29T01:06:20-08:00", 
    "directory": "/Stats_subscription-For-Demo-Do not delete/WUS-Stats_subscription-EyeBall", 
    "error_bytes_free_size": null, 
    "error_percent_free": null, 
    "home_share": false, 
    "id": 1408, 
    "name": "WUS-Stats_subscription-EyeBall", 
    "node_id": 5, 
    "percent_free": null, 
    "status": null, 
    "status_at": "2016-12-06T21:24:28-08:00", 
    "status_message": null, 
    "updated_at": "2016-12-06T21:24:28-08:00", 
    "warn_bytes_free_size": null, 
    "warn_percent_free": null 
    }, 
    { 
    "browse_count": 0, 
    "bytes_free_string": null, 
    "bytes_total_string": null, 
    "bytes_unknown": true, 
    "comment_added_at": null, 
    "content_added_at": null, 
    "created_at": "2016-11-29T01:06:50-08:00", 
    "directory": "/Stats_subscription-For-Demo-Do not delete/WUS-Stats_subscription-Goku", 
    "error_bytes_free_size": null, 
    "error_percent_free": null, 
    "home_share": false, 
    "id": 1409, 
    "name": "WUS-Stats_subscription-Goku", 
    "node_id": 5, 
    "percent_free": null, 
    "status": null, 
    "status_at": "2016-12-06T21:24:29-08:00", 
    "status_message": null, 
    "updated_at": "2016-12-06T21:24:29-08:00", 
    "warn_bytes_free_size": null, 
    "warn_percent_free": null 
    }, 
    { 
    "browse_count": 0, 
    "bytes_free_string": null, 
    "bytes_total_string": null, 
    "bytes_unknown": true, 
    "comment_added_at": null, 
    "content_added_at": null, 
    "created_at": "2016-11-29T01:07:18-08:00", 
    "directory": "/Stats_subscription-For-Demo-Do not delete/WUS-Stats_subscription-Cybersecurity", 
    "error_bytes_free_size": null, 
    "error_percent_free": null, 
    "home_share": false, 
    "id": 1410, 
    "name": "WUS-Stats_subscription-Cybersecurity", 
    "node_id": 5, 
    "percent_free": null, 
    "status": null, 
    "status_at": "2016-12-06T21:24:30-08:00", 
    "status_message": null, 
    "updated_at": "2016-12-06T21:24:30-08:00", 
    "warn_bytes_free_size": null, 
    "warn_percent_free": null 
    }, 
    { 
    "browse_count": 0, 
    "bytes_free_string": null, 
    "bytes_total_string": null, 
    "bytes_unknown": true, 
    "comment_added_at": null, 
    "content_added_at": null, 
    "created_at": "2016-11-29T01:08:00-08:00", 
    "directory": "/Stats_subscription-For-Demo-Do not delete/WUS-Stats_subscription-Feature_Phones_Dubai SOR", 
    "error_bytes_free_size": null, 
    "error_percent_free": null, 
    "home_share": false, 
    "id": 1411, 
    "name": "WUS-Stats_subscription-Feature_Phones_Dubai SOR", 
    "node_id": 5, 
    "percent_free": null, 
    "status": null, 
    "status_at": "2016-12-06T21:24:31-08:00", 
    "status_message": null, 
    "updated_at": "2016-12-06T21:24:31-08:00", 
    "warn_bytes_free_size": null, 
    "warn_percent_free": null 
    }, 
    { 
    "browse_count": 0, 
    "bytes_free_string": null, 
    "bytes_total_string": null, 
    "bytes_unknown": true, 
    "comment_added_at": null, 
    "content_added_at": null, 
    "created_at": "2016-11-29T01:10:47-08:00", 
    "directory": "/Stats_subscription-For-Demo-Do not delete/WUS-Stats_subscription-Nimbus", 
    "error_bytes_free_size": null, 
    "error_percent_free": null, 
    "home_share": false, 
    "id": 1412, 
    "name": "WUS-Stats_subscription-Nimbus", 
    "node_id": 5, 
    "percent_free": null, 
    "status": null, 
    "status_at": "2016-12-06T21:24:32-08:00", 
    "status_message": null, 
    "updated_at": "2016-12-06T21:24:32-08:00", 
    "warn_bytes_free_size": null, 
    "warn_percent_free": null 
    }, 
    { 
    "browse_count": 0, 
    "bytes_free_string": null, 
    "bytes_total_string": null, 
    "bytes_unknown": true, 
    "comment_added_at": null, 
    "content_added_at": null, 
    "created_at": "2016-11-30T15:49:52-08:00", 
    "directory": "/EPRS-Connect/HK-EPRS-NVIDIA", 
    "error_bytes_free_size": null, 
    "error_percent_free": null, 
    "home_share": false, 
    "id": 1467, 
    "name": "HK-EPRS-NVIDIA", 
    "node_id": 7, 
    "percent_free": null, 
    "status": null, 
    "status_at": "2016-12-06T21:24:34-08:00", 
    "status_message": null, 
    "updated_at": "2016-12-06T21:24:34-08:00", 
    "warn_bytes_free_size": null, 
    "warn_percent_free": null 
    } 
] 

、あなたは私はあなたが読みやすい形式でコンソールにそれを表示/印刷したいと思いますこの文からto deserialize it in readable format.を尋ねました。

これを実現するにはNewtonsoft.Jsonライブラリを利用することができます。Json.NETナゲットパッケージをプロジェクトに追加し、Console.WriteLine(responseFromServer);ステートメントの直前のコードに追加する必要があります。

JToken jsonToken = JToken.Parse(responseFromServer); 
responseFromServer = jsonToken.ToString(Newtonsoft.Json.Formatting.Indented); 

これはJSONが読み取り可能な形式でコンソールウィンドウに表示/印刷されます。

+0

こんにちはVedant ...どのコードを実行しましたか?私かhsimahによって提供されたもの? – dotnetman

+0

@ dotnetmanあなたから提供されたものです。答え(JSON)に表示されている出力は読み込み可能な形式ではありません。 – Vedant

+0

@dotnetmanあなたは、** Json.Net ** nugetパッケージをあなたのプロジェクトと2行のコードに追加するだけで、フォーマットされていないjsonを読み込み可能な(フォーマットされた)jsonに変換するコンソールに出力する必要があります。 – Vedant

関連する問題