我正在使用一个以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);
}
我已经执行了您的代码,看来它正在返回并打印在控制台上的以下数据(不可读格式(缩进))。
[
{
"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
Nuget软件包添加到您的项目中,并在Console.WriteLine(responseFromServer);
语句之前遵循代码。
JToken jsonToken = JToken.Parse(responseFromServer);
responseFromServer = jsonToken.ToString(Newtonsoft.Json.Formatting.Indented);
这将以可读格式在控制台窗口上打印/显示JSON。
这应该做到:
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个,因为它是最长的密钥长度。您可以使用此代码来制作填充变量。