【发布时间】:2013-07-11 22:34:31
【问题描述】:
我是 Windows Phone 开发新手。我正在开发一个从 Web 服务获取 json 并对其进行解析并将其显示给应用程序的应用程序。我使用 json.net 来解析它。这是我的 json 文件:
[
{
"id": "001",
"title": "title1",
"content": "sample content",
"category_id": "3",
"image": "defaultimg.jpg"
},
{
"id": "021",
"title": "title2",
"content": "sample content",
"category_id": "1",
"image": "defaultimg2.jpg"
},
{
"id": "011",
"title": "title3",
"content": "sample content",
"category_id": "3",
"image": "defaultimg22.jpg"
},
{
"id": "008",
"title": "title24",
"content": "sample content",
"category_id": "2",
"image": "defaultimg12.jpg"
},
{
"id": "121",
"title": "title12",
"content": "sample content",
"category_id": "3",
"image": "defaultimg27.jpg"
}
]
所以我在 json2csharp.com 的帮助下想出了这个类
public class RootObject
{
public string id { get; set; }
public string title { get; set; }
public string content { get; set; }
public string category_id { get; set; }
public string image { get; set; }
}
这是我的cs代码
var data = new WebClient();
Observable
.FromEvent<DownloadStringCompletedEventArgs>(data, "DownloadStringCompleted")
.Subscribe(r =>
{
var deserialized =
JsonConvert.DeserializeObject<List<RootObject>>(r.EventArgs.Result);
ListBox1.ItemsSource = deserialized;
});
data.DownloadStringAsync(
new Uri("http://sampleurl.com/xyz/myjson.aspx"));
我只想显示那些在 listbox1 上有 "category_id": "9" 的人 你能帮我如何过滤这些数据吗?我是 c# windows phone 的学生和新手。谢谢!
【问题讨论】:
-
您遇到的错误是什么?您能提供您尝试显示它们的代码吗?
-
我刚刚尝试了@jbabey 给我的语法:ListBox1.ItemsSource = deserialized.Where(r => r.category_id == 9); “不能在此范围内声明名为 'r' 的局部变量,因为它会给 'r' 赋予不同的含义,后者已在'父或当前'范围内用于表示其他内容”
-
好吧,酷!您始终可以使用“编辑”链接按钮将这些信息添加到您的原始帖子中。人们通常用粗体写“编辑”,然后添加缺少的信息 :) 请记住,您始终可以编辑自己的帖子 :) 欢迎使用 SO btw,如果您想了解有关社区的更多信息,我建议您也看看我们的帮助部分:stackoverflow.com/help 祝你好运。
标签: c# windows-phone-7 json.net