Whats Going On
Whats Going On

Reputation: 1397

windows phone Json parsing form a URL to async listbox?

I am a android developer, I am new to windows phone,here I followed this to handle list box in windows phone7/8.

But here I tried to Parse JSON form a URL,actually that above example is for XML in async list-box but here i need to do Json form a URL to async list-box(list-view).

this it the code i used for receive from URL

private void FlickrSearch_Click(object sender, RoutedEventArgs e)
    { 
      WebClient webclient = new WebClient(); 
      webclient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webclient_DownloadStringCompleted); 
      webclient.DownloadStringAsync(new Uri("http://api.flickr.com/services/feeds/photos_public.gne?tag=" + SearchBox.Text + "&format=rss2")); // Flickr search 
  }

But I need to use this for JSON form a Async list-box

anyone please suggest me for JSON from a URL, in the part of Windows phone.

Upvotes: 0

Views: 422

Answers (1)

har07
har07

Reputation: 89315

Actually, this question is a bit broad for complete detailed answer. But assuming you already managed to get it work with XML data by following the above mentioned blog post, you can do some adjustment to make the same works with JSON.

  1. Install Json.Net from nuget
  2. Inspect json format returned by the API by opening the API search url in browser. Don't forget to change format specification to json, for example http://api.flickr.com/services/feeds/photos_public.gne?tag=car&format=json. There you'll see that json string returned isn't valid. Need a bit of manual clean up to make it valid : remove "jsonFlickrFeed(" at the beginning and ")" at the end of json string. Then the string is ready to be deserialized using Json.Net.
  3. The rest should be the same as explained in the original blog post

Following is code snippet for the part of handling JSON data :

using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

void webclient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) 
{ 
   if (e.Error != null)
    { 
       MessageBox.Show("error");
       return;
    } 

    var jsonString = e.Result;

    //clean up json string to make it valid json
    jsonString = jsonString.Replace("jsonFlickrFeed(", "");
    jsonString = jsonString.Remove(jsonString.Length - 1);

    //deserialize json string to object
    var rootObject = JsonConvert.DeserializeObject<JObject>(jsonString);
    var items = (JArray)rootObject["items"];

    //populate listbox with items from json
    listBox1.ItemsSource = from tweet in items                                     
    select new FlickrData                                   
     { 
       ImageSource = tweet["media"]["m"].ToString(), 
       Message = tweet["description"].ToString(), 
       UserName = tweet["title"].ToString(), 
       PubDate = DateTime.Parse(tweet["published"].ToString())                                    
     };                 
}

Upvotes: 1

Related Questions