Skip to main content

How to parse JSON data with .Net C#?

There are many occasions arise when we have to get some information form websites, websites offer information to their user in different formats and JSON is one of them.

Actually you can parse JSON data in C# itself by writing some code but JSON.Net library will help a great deal.

Example 
Let's consider an example where we have to get tweets count of a webpage from twitter. Twitter provides us with an easy way to do that by simply sending a request to one of its urls

Request Sample 
https://cdn.api.twitter.com/1/urls/count.json?url=http://download.cnet.com/Free-PDF-to-JPG-Converter/3000-2016_4-75728284.html


Request Response


string Response = {"count":10,"url":"http:\/\/download.cnet.com\/Free-PDF-to-JPG-Converter\/3000-2016_4-75728284.html\/"}



Here we go, we need to get count variable from that response.

Create a Class in C# and add this code

 public class SharesCount
 {
        private int Count_;

        public int Count
        {
            get { return Count_; }
            set { Count_= value; }
        }
 }

Here you notice that text in orange color, right?

Yeah you need to make sure that class properties match the variable (Count) that we want to get from response.

string src = Response;
SharesCount m = JsonConvert.DeserializeObject<SharesCount>(src);
int count = m.Shares;


This post answer these question

How to use Newtonsoft.Json?
How to use Json.Net?
How to get Twitter tweet count for a url?

Comments

Popular posts from this blog

World's best source code hosting websites

1-Google Code Hosting code.google.com Google code is the most popular hosting site which hosts the likes of jQuery and other popular libraries. 2-Github Github Next in the line is Github, it provides fast and easy hosting which version support and as well as code monitoring. 3-Sourceforge Sourceforge Not much of a code hosting site but you can create project and is the leading open source hosting site which well supports all type of languages. 4-Launchpad Launchpad Launchpad is an online collaboration platform that provides services like bug tracking, version control , code reviews and much more. 5-Codeplex Codeplex A popular hosting site hosted by microsoft so not much features you can expect. 6-Bitbucket Bitbucket Develop code as a team. Keep all your development in one place, be it issue tracking, documentation or sharing code. 7-Assembla Assembla Not a free code hosting and sharing site, though excellent features such at agile development tools makes it well wo...

Make the text on your screen easier to read in windows7

Right click on Desktop... And then click Personalize on menu... And then follow these steps...

When your windows was installed on your computer

(Start -> Run -> cmd), and enter  systeminfo