我正在使用以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);
}
答案 0 :(得分: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
),将其反序列化为字典并选择密钥。我选了24,因为它是最长的密钥长度。您可以使用this code来填充变量。
答案 1 :(得分: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
nuget包添加到项目中,并在Console.WriteLine(responseFromServer);
语句之前使用代码。
JToken jsonToken = JToken.Parse(responseFromServer);
responseFromServer = jsonToken.ToString(Newtonsoft.Json.Formatting.Indented);
这将以可读格式在控制台窗口上打印/显示JSON。