【发布时间】:2013-12-13 22:27:12
【问题描述】:
我有一个提供JSON 对象的PHP Web 服务。在我的 Windows Phone 8 应用程序中,我通过执行以下操作来使用该服务:
public void GetSigns()
{
var webClient = new WebClient();
var uri = new Uri("someURL");
webClient.OpenReadCompleted += GetSigns_Completed;
webClient.OpenReadAsync(uri);
}
private void GetSigns_Completed(object sender, OpenReadCompletedEventArgs e)
{
using (var sr = new StreamReader(e.Result))
{
var data = sr.ReadToEnd();
var result = JsonConvert.DeserializeObject<List<GetSignsResponse>>(data);
}
}
using 语句中的 data 对象已正确填充 JSON 字符串。但是,结果对象会抛出异常(我已经删除了 try 和 catch 以使代码更具可读性)。
这就是我的GetSignsResponse 对象的样子:
public class GetSignsResponse
{
public int ID { get; set; }
public string Online { get; set; }
public string Location { get; set; }
public DateTime Maxupdated { get; set; }
public string Line1 { get; set; }
public string Line2 { get; set; }
public string Line3 { get; set; }
public string Line4 { get; set; }
}
JSON 返回以下内容:
Web 服务返回 GetSignsResponse 对象的集合。
知道为什么GetSigns_Completed 回调方法中的result 对象不能正常工作吗?我的GetSignsResponse 对象是否缺少某些东西?
我得到的 错误 是它未能将 JSON 转换为 GetSignsResponse 的 List。
更新 Web 服务 JSON 如下所示:
{
sign: {
id: "11",
online: "1",
location: "abc",
maxupdated: "2013-11-19 16:59:05",
line1: " abc ",
line2: " abc ",
line3: " abc ",
line4: " "
}
},
{
sign: {
id: "7",
online: "1",
location: "abc",
maxupdated: "2013-11-19 16:58:03",
line1: " abc ",
line2: " abc ",
line3: " abc ",
line4: " "
}
},
GetSigns_Completed 中的 data 对象如下所示:
"\t{\"signs\":[{\"sign\":{\"id\":\"1\",\"online\":\"1\",\"location\ ":\"abc\",\"maxupdated\":\"2013-10-05 06:29:02\",\"line1\":\"\",\"line2\":\"\" ,\"line3\":\"\",\"line4\":\"\"}},{\"sign\":{\"id\":\"2\",\"online\" :\"1\",\"位置\":\"abc\",\"maxupdated\":\"2013-10-05 18:21:01\",\"line1\":\"abc\ ",\"line2\":\" abc \",\"line3\":\" abc \",\"line4\":\" \"}},{\"sign\""}}]} "
【问题讨论】:
-
json 在哪里?
-
嗨 L.B. - 我已经更新了原来的帖子。底部部分仅包含 JSON 字符串中的两个对象。
-
Subby 您如何期望我们在不知道 正确 json 的情况下给出正确答案(不是您发布的那样)。
-
@L.B 对不起,我想我现在明白你的意思了。我已经更新了帖子。
标签: c# json web-services