【发布时间】:2012-10-14 01:34:15
【问题描述】:
我正在尝试进行 POST,然后将 JSON 响应读入字符串。
我认为我的问题是我需要将自己的对象传递给 DataContractJsonSerializer,但我想知道是否有某种方法可以将响应转换为关联数组或某种键/值格式。
我的 JSON 格式如下:{"license":"AAAA-AAAA-AAAA-AAAA"} 我的代码如下:
using (Stream response = HttpCommands.GetResponseStream(URL, FormatRegistrationPost(name, email)))
{
string output = new StreamReader(response).ReadToEnd();
response.Close();
DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(string));
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(output));
string results = json.ReadObject(ms) as string;
licenseKey = (string) results.GetType().GetProperty("license").GetValue(results, null);
}
【问题讨论】:
-
Newtonsoft JSON 可以反序列化为 Dictionary .. 并且可以很容易地导航。