如何从WebClient响应中对数据进行排序

时间:2014-07-20 15:22:10

标签: c# json windows-phone-8 webclient

我有一个像这样的WebClient:

private void Button_Click(object sender, RoutedEventArgs e)
{
    WebClient wc = new WebClient();
    wc.DownloadStringAsync(new Uri(myurl));
    wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
}

处理程序:

private void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    MessageBox.Show(e.Result.ToString());
}

这就是我得到的:

    {"out_error":"",
"out_id":4274,
"out_device_hash":"7a8e4f7a264a6afb38cfadb6b50e7a45c58d226b",
"out_device_name":"Unnamed device",
"out_uname":"User Name",
"token":"e1c063d16fee7bb8912099034f67c2d17a8f45c3"}

问题是如何将这些字符串排序为单独的字符串,如xml:

string error = out_error;
string id = out_id;

依此类推......谢谢。

1 个答案:

答案 0 :(得分:2)

使用Json解析器。

使用Json.Net

var dict1 = JsonConvert.DeserializeObject<Dictionary<string, string>>(e.Result);

使用JavaScriptSerializer

var dict2 = new JavaScriptSerializer()
            .Deserialize<Dictionary<string, string>>(e.Result);


string id = dict1["out_id"];