Reputation: 1397
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
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.
"jsonFlickrFeed("
at
the beginning and ")"
at the end of json string. Then the string is
ready to be deserialized using Json.Net.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