C#: deserializing JSON
In this tutorial we will learn how to deserialize JSON in C# using the Json.net library.
(more…)
View On WordPress

seen from Russia

seen from United Kingdom

seen from United Kingdom
seen from United States
seen from United Kingdom

seen from Brazil
seen from Türkiye
seen from United Kingdom
seen from Malaysia
seen from India
seen from China
seen from Germany

seen from United States

seen from Australia

seen from China

seen from Angola
seen from China
seen from Poland
seen from United States

seen from Germany
C#: deserializing JSON
In this tutorial we will learn how to deserialize JSON in C# using the Json.net library.
(more…)
View On WordPress
Deserialize Json data using Newtonsoft Json.NET library
Deserialize Json data using Newtonsoft Json.NET library
As we learn to make more and more advanced apps, we need more tools in out kitty to make an amazing app. Local apps work great but they can only do so much. Exchanging data from a server or web service is very essential to make a dynamic and constantly updated app. Let’s see how to work with Json in Windows Phone 8 app using the Json.NET library.
What is JSON?
JSON(JavaScript Object Notation) is…
View On WordPress
How to query Freebase from c#...
Freebase is a great source of open data,we can query it using MQL.I will try and show how we can quickly query it to get some data.
Firstly i used .net 4 ,visual studio sp1,nuget(Awesome package management) and two cool packages(details about the packages follow).
The objective is to retrieve a set of albums of "The Police".
class Album
{ public String type {get;set;}
public String name {get;set;}
public String[] album { get; set; }
}
Notice that we need to get a set of "albums" for the artist specified by "name".The search criteria also needs a domain to look into which we will specify through "type".
Next,We need to set up a Json Object and send it across as a parameter over HTTP.We can consume the Rest based service through a wonderful nuget package RestSharp.
var client = new RestClient();
client.BaseUrl = "https://www.freebase.com/api/service/mqlread";
var request = new RestRequest(Method.GET);
Now,we need to create a album object to pass along with the request.
Album thepolice = new Album{type = "/music/artist",name = "The Police" };
Next we need to serialize the object,so we will use NewtonSoft(which you won't need to install since it is dependency for the RestSharp package and nuget will fetch it beforehand).
Freebase follows the fill in the blanks approach to return data i.e. for the following MQL query
{
"id": "/en/new_york",
"guid": null,
"name": null,
"/location/location/containedby": [],
"/location/place_with_neighborhoods/neighborhoods": []
}
Freebase will return results where ever it finds null(name,guid) or [](/location/location/containedby,/location/place_with_neighborhoods/neighborhoods).The places with null will be substituted with a single result and [] will be substituted with a set of results.
So we now have to convert our album object into a Json Object ans attach it to our request.Initially we add the following method to the Album class.
class Album {
public String type {get;set;}
public String name {get;set;}
public String[] album { get; set; }
public String ToJsonString(){
return "{" +
JsonConvert.SerializeObject("query", Formatting.Indented) + ":" +
JsonConvert.SerializeObject(this, Formatting.Indented) + "\n" +
"}";
}
}
We now initialize our album object.
Album thepolice = new Album{type = "/music/artist",name = "The Police" };
The following code adds the query parameter and calls the service.
Console.WriteLine(thepolice.ToJsonString());//Just for debug puposes
request.AddParameter("query", thepolice.ToJsonString());
request.RequestFormat = DataFormat.Json;
RestResponse response = client.Execute(request);
Console.WriteLine("My output :" +"\n"+response.Content);
The output we get is as follows:
{"query":{
"type": "/music/artist",
"name": "The Police",
"album": null
}
}
My output :
{
"code": "/api/status/error",
"messages": [
{
"code": "/api/status/error/mql/result",
"info": {
"count": 31,
"result": [
"Outlandos d'Amour",
"Reggatta de Blanc",
"Zenyatt\u00e0 Mondatta",
"Ghost in the Machine",
"Synchronicity",
"Every Breath You Take: The Singles",
"Greatest Hits",
"Message in a Box: The Complete Recordings",
"Live!",
"Every Breath You Take: The Classics",
"Their Greatest Hits",
"Can't Stand Losing You",
"Roxanne '97 (Puff Daddy remix)",
"Roxanne '97",
"The Police",
"Greatest Hits",
]
},
"message": "Unique query may have at most one result. Got 31",
"path": "album",
"query": {
"album": null,
"error_inside": "album",
"name": "The Police",
"type": "/music/artist"
}
}
],
"status": "200 OK",
"transaction_id": "cache;cache04.p01.sjc1:8101;2011-09-06T20:48:46Z;0011"
}
Note carefully two things:-
1. The value of null instead of [] for album,this justifies the message we get in the output
2.If your query is wrong the status code will change to 400,here the code is 200 indicating the request is good.
Now let us try a quick fix of the problem
The method will be tweaked to make way for the [] instead of null to capture a set of albums instead of a single album.
public String ToJsonString(){
return "{" +
JsonConvert.SerializeObject("query", Formatting.Indented) + ":" +
"{" + "\n" +
JsonConvert.SerializeObject("type", Formatting.Indented) + ":" +
JsonConvert.SerializeObject(this.type, Formatting.Indented) + "," + "\n" +
JsonConvert.SerializeObject("name", Formatting.Indented) + ":" +
JsonConvert.SerializeObject(this.name, Formatting.Indented) + "," + "\n" +
JsonConvert.SerializeObject("album", Formatting.Indented) + ":" +
"[]" + "\n" +
"}" + "\n" +
"}";
}
The output is as follows:-
{"query":{
"type":"/music/artist",
"name":"The Police",
"album":[]
}
}
My output :
{
"code": "/api/status/ok",
"result": {
"album": [
"Outlandos d'Amour",
"Reggatta de Blanc",
"Zenyatt\u00e0 Mondatta",
"Ghost in the Machine",
"Synchronicity",
"Every Breath You Take: The Singles",
"Greatest Hits",
"Message in a Box: The Complete Recordings",
"Live!",
"Every Breath You Take: The Classics",
"Their Greatest Hits",
"Can't Stand Losing You",
"Roxanne '97 (Puff Daddy remix)",
"Roxanne '97",
"The Police",
"Greatest Hits",
],
"name": "The Police",
"type": "/music/artist"
},
"status": "200 OK",
"transaction_id": "cache;cache04.p01.sjc1:8101;2011-09-06T21:02:11Z;0035"
}
Notice how everything becomes well and the error message goes away and the code in the first line changes from error to OK.
The next logical step would be to generate a helper method to give us a JsonString for all properties including lists,dictionary etc.I will try and address that in the next blog post.Hope this helps you start with Freebase.
I have omitted some set of results(not 31 if you count them :P) for lack of space.