Aayushi Soni
Aayushi Soni

Reputation: 489

How to read json file format and how to write the json in c# asp.net

In my ASP.NET MVC project I am conecting to a remote cloud server which is returning the data in json like:

[{
    "name":"new-service-dev",
    "Isenabled":"true",
    "ttl":86400,
    "cdn_uri":"http://c0099.cdn2.files.rackspacecloud.com",
    "referrer_acl":"",
    "useragent_acl":"", 
    "log_":"false"
}]

Now I want to get all the values in list or array format, for example I want to get "cdn_uri".

I also want to create JSON somewhere in my code, how do I create and write JSON?

Upvotes: 0

Views: 3918

Answers (2)

Simon Steele
Simon Steele

Reputation: 11608

You can use the JSON.Net component from codeplex:

http://json.codeplex.com/

This will let you read/write JSON. Here's a simple example using your JSON from the question:

    static void Main(string[] args)
    {
        JObject o =
            JObject.Parse(
                "{    \"name\":\"new-service-dev\",    \"Isenabled\":\"true\",    \"ttl\":86400,    \"cdn_uri\":\"http://c0099.cdn2.files.rackspacecloud.com\",    \"referrer_acl\":\"\",    \"useragent_acl\":\"\",     \"log_\":\"false\"}");

        string cdn_uri = (string)o.SelectToken("cdn_uri");
        Console.WriteLine(cdn_uri);
        Console.ReadKey();
    }

Upvotes: 2

Prashant Lakhlani
Prashant Lakhlani

Reputation: 5806

asp.net has an extension dll called system.web.extensions which is having support for javascript and json serialization. see this link

Upvotes: 1

Related Questions