Home > Uncategorized > Parse JSON in WP7

Parse JSON in WP7

Windows Phone 7 may be quite obsolete at this point, but since WP8 still runs WP7 apps, it is worth trying to make your apps backwards-compatible where possible. 

Using Newtonsoft JSON.NET to parse JSON is a good option, but you have to get the right version, the latest version is only for Windows Phone 8. I found version 4.0.3 (40r3)  worked for me. 

Here is a brief example of how I used it:

public static void Search(string depart, string arrive, Action<JObject> callback)
{
var strUrl = “http://www.rome2rio.com/api/1.0/json/Search?key=xxxx&oName={0}&dName={1}”;
strUrl = string.Format(strUrl, depart, arrive);
WebClient wc = new WebClient();
wc.DownloadStringAsync(new Uri(strUrl), callback);
wc.DownloadStringCompleted += (sender, args) =>
{
var jo = JObject.Parse(args.Result);
callback(jo);
};
}

Of course, you need to use using Newtonsoft.Json.Linq;

Advertisements
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: