简体   繁体   中英

Deserializing a stream of JSON Output

I am using an API that provides the output in JSON format and I am sure about the fields that it contains. I want to deserialize it into a list format. I went through the Newtonsoft.Json namespace but didn't get much help. The following article was good but it didn't serve my purpose as I am not aware of the key/value pairs. Article: http://www.newtonsoft.com/json/help/html/deserializeobject.htm

My code:

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

This should do it:

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($"----------------------");
}

This parses the payload as a JArray , grabs the first item (check for null first), deserialises it into a dictionary and selects the keys. I picked 24 as it's the longest key length. You could make the padding variable by using this code .

I have executed your code and it seems that it is returning and printing the following data on console(not in readable format(indented)).

[
  {
    "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
  }
]

And You asked to deserialize it in readable format. From this statement i think you want to print/show it on the console in readable format.

To achieve this you can make use of Newtonsoft.Json library all you have to add the Json.NET nuget package to your project and following code just before the Console.WriteLine(responseFromServer); statement.

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

This will print / show the JSON on console window in readable format.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM