0

私は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 の初心者です。ありがとう!

4

2 に答える 2

1

通常、LINQList<RootObject>を使用して、次のような操作を行います。

var deserialized = JsonConvert.DeserializeObject<List<RootObject>>(r.EventArgs.Result);

// select only RootObjects with category_id equal to 9
ListBox1.ItemsSource = deserialized.Where(r => r.category_id == 9);
于 2013-07-11T14:33:34.783 に答える