0

I am new to windows phone dev. I'm working on an app that fetch json from a web service and parse it and display it to the app. I used json.net to parse it. here's my json file:

[
{
    "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"
}
]

so I came up with this class with the help of 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; }
}

here's my code in 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"));

I want to display only those who has "category_id": "9" on the listbox1 can you help me how to filter this data? im a student and new in c# windows phone. Thanks!

3
  • What is the error you are getting? Can you provide the code you have tried to display them? Commented Jul 11, 2013 at 14:49
  • I just tried this syntax what @jbabey gave me: ListBox1.ItemsSource = deserialized.Where(r => r.category_id == 9); "A local variable named 'r' cannot be declared in this scope because it would give a different meaning to 'r', which is already used in a 'parent or current' scope to denote something else" Commented Jul 11, 2013 at 15:03
  • Ok, cool! You can always use the "edit" link button to add those information into your original post. People usually write "Edit" in bold and then add missing info :) Remember you can always Edit your own post :) Welcome on SO btw, I suggest you also have a look to our Help section if you want to learn more about the community : stackoverflow.com/help Good Luck. Commented Jul 11, 2013 at 15:22

2 Answers 2

1

You would generally want to use LINQ to manipulate your List<RootObject>, something like:

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Hi @jbabey, I tried your syntax and it gives me this error: A local variable named 'r' cannot be declared in this scope because it would give a different meaning to 'r', which is already used in a 'parent or current' scope to denote something else
@LadzAngeles then change it to a different name.
0

If the API itself does not have an endpoint that allows you to query for only those records with category_id : 9 then you'll have to do the filtering at the client to populate your listbox. One common and easy way to do that is with LINQ.

Here is an example of what the LINQ syntax would look like:

  var categoryNineOnly = data.Where(x=>x.category_id == 9)

Much more detail here:

http://msdn.microsoft.com/en-us/library/vstudio/bb397926.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.