我有这段代码:
string responseFromServer;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (Stream dataStream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(dataStream))
{
responseFromServer = reader.ReadToEnd();
}
}
}
responseFromServer是一个字符串类型变量,包含这个“key:value”数据:
{"refresh_token":"69d2b7bq95b6sf5b64c55240ed563a52","expires_in":86400,"access_token":"0q761ee1897hd50u2r4fec80f333dd43","token_type":"bearer","x_mailru_vid":"13090076762971691053"}
这个字符串我需要转换为这个字符串数组:
refresh_token = 69d2b7bq95b6sf5b64c55240ed563a52
expires_in = 86400
access_token = 0q761ee1897hd50u2r4fec80f333dd43
token_type = bearer
x_mailru_vid = 13090076762971691053
实施它的最简单方法是什么? 提前谢谢。
答案 0 :(得分:3)
这是一个json字符串。您可以使用任何json序列化程序。我将使用Json.Net
var result = JsonConvert.DeserializeObject<Result>(json);
public class Result
{
public string refresh_token { get; set; }
public int expires_in { get; set; }
public string access_token { get; set; }
public string token_type { get; set; }
public string x_mailru_vid { get; set; }
}
您也可以反序列化为字典。
var dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
内置JavaScriptSerializer的另一种选择
var dict = new JavaScriptSerializer().Deserialize<Dictionary<string, string>>(json);