我想從使用JSON.Net的Web服務中獲取一些JSON數據。我得到的錯誤是一個意想不到的字符,同時解析json數據。我使用的代碼如下:在WP7上使用JSON.net反序列化JSON
HttpWebRequest request;
WebResponse response;
private void btnGet_Click(object sender, RoutedEventArgs e)
{
request = WebRequest.Create(@"http://http://domain.com/test/question.php") as HttpWebRequest;
request.BeginGetResponse(AfterRequest, null);
}
private void AfterRequest(IAsyncResult result)
{
response = request.EndGetResponse(result);
using (StreamReader sd = new StreamReader(response.GetResponseStream()))
{
string resultString = sd.ReadToEnd();
Dictionary<string, string> values = JsonConvert.DeserializeObject<Dictionary<string, string>>(resultString);
response.Close();
MessageBox.Show(values["Question"]);
}
}
數據我試圖反序列化是:
{"Question":"How old am i?","A":"20","B":"23","C":"25","D":"26","Z":"D"}
這些數據通過PHP/MySQL的輸出。任何想法,如果它是我的代碼或JSON數據是無效的?
謝謝 編輯:我已經更新了我想要反序列化的數據,它現在看起來像這樣; { "Question": "How old am i?", "Answers": { "A": "24", "B": "25", "C": "26", "D": "27" }, "Answer": "B" }
您是否調試過並看到「resultString」變量具有該json數據? – Numenor 2011-04-29 16:20:20
我會建議編寫一個測試程序來輸出所需的json,當你設法解決這個問題時,從json.net – AwkwardCoder 2011-05-03 22:54:24
序列化?如果是的話,請在這裏發佈解決方案。謝謝。 – Paul 2011-08-23 13:42:27