Get JSON Data

JSON (JavaScript Object Notation) is a lightweight data-interchange format. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others (http://www.json.org/)

How to get those data and deserialize them into our object data ?
You can check out the following C# code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using System.Net;
using System.IO;
using System.Web.Script.Serialization;
namespace JsonConsoleApplication
{
class Program
{
private const string apiUrl = "http://www.example.com";
private const string loginname = "xxxx";
private const string password = "****";
static void Main(string[] args)
{
WebClient webClient = new WebClient();
string url = apiUrl;
webClient.Credentials = new System.Net.NetworkCredential(loginname,password);
var json = webClient.DownloadString(url);
JavaScriptSerializer parser = new JavaScriptSerializer();
var info = parser.Deserialize<List<Track>>(json);
List<Track> listTrack = (List<Track>)info;
//note that List<Track> is an object, you can change it by yours
}
}
}
view raw getjson.cs hosted with ❤ by GitHub
Please note that Track object properties should be configured as same as the JSON return value. As such, before create the object data, you must generate c# classes from JSON. You may use jsonTocsharp by simply entering the JSON, and klik generate, you will get a bunch properties that should be put within your class/object.

For further knowledge about JSON, you can visit Mastering JSON

May it helps,
Thanks

No comments:

Post a Comment