【问题标题】:Deserializing a stream of JSON Output反序列化 JSON 输出流
【发布时间】:2016-12-07 05:16:24
【问题描述】:

我正在使用以 JSON 格式提供输出的 API,并且我确信它包含的字段。我想将其反序列化为列表格式。 我浏览了 Newtonsoft.Json 命名空间,但没有得到太多帮助。下面的文章很好,但它没有达到我的目的,因为我不知道键/值对。 文章:http://www.newtonsoft.com/json/help/html/deserializeobject.htm

我的代码:

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

【问题讨论】:

  • “我想将其反序列化为可读格式。” - JSON 已经是人类可读的格式,因为它“只是”一个字符串。请edit您的问题以显示您所需输出格式的示例。您尝试使用哪种语言,JavaScript 还是 C#?
  • 通过可读格式,我的意思是在列表中,即一列中的所有键和另一列中的相应值。
  • @dotnetman 编辑帖子时,您无需在标题中添加“编辑 1”。每个帖子都有一个edit history,表明它已被编辑以及发生了什么变化。每个人都可以看到任何帖子的编辑历史记录。

标签: javascript c# json asp.net-mvc json.net


【解决方案1】:

我已经执行了你的代码,它似乎正在控制台上返回并打印以下数据(不是可读格式(缩进))。

[
  {
    "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.Jsonlibrary 将 Json.NET nuget 包添加到您的项目中,并在 Console.WriteLine(responseFromServer); 语句之前添加以下代码。

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

这将以可读格式在控制台窗口上打印/显示 JSON。

【讨论】:

  • 嗨 Vedant...您执行了哪些代码?是我提供的还是hsimah提供的?
  • @dotnetman 您提供的那个。我在答案中显示的输出(JSON)不是可读格式。
  • @dotnetman 您只需将 Json.Net nuget 包添加到您的项目和两行代码,然后打印到控制台,这将转换未格式化的 json变成可读(格式化)的 json。
【解决方案2】:

应该这样做:

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),将其反序列化为字典并选择键。我选择了 24,因为它是最长的密钥长度。您可以使用this code 来制作填充变量。

【讨论】:

    猜你喜欢
    • 2017-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-19
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多