Amrit
Amrit

Reputation: 421

JSON human readable proper formatting

Hi I have a quick question regarding json responses from the web services, I built a restful WCF service to return JSON response but the response is all messy as it a Ilist<object> with 60 counts. How can i format this array like google or yahoo APIs to make them easily readable e.g

Upvotes: 0

Views: 1940

Answers (4)

Mr.Deer
Mr.Deer

Reputation: 476

I post this answer because could be usefull for future users. I went into this question and finally this code solved my case.

In C# if you use Newtonsoft library you can simply serialize the JSON with indented format using one line.

JsonConvert.SerializeObject(objectToSerialize,Formatting.Indented);

Upvotes: 1

Habib
Habib

Reputation: 223332

I am not too sure why you want to format your Json response, it should be your service client which should do the formatting if required. You am look at the Json Pretty Printer/Beautifier Library For .Net, which takes Json string as input and return a formatted Json string.

Upvotes: 1

Govind Malviya
Govind Malviya

Reputation: 13753

I think no need to format it. it's very good to use compressed json. But you can still do that check this question. Otherwise write in your docs that you can use some online formatter to format json. I worked with json restfull api and I prefer to use compresses json.

  1. Json formatter
  2. Json tree viewer

Upvotes: 1

Jason Goemaat
Jason Goemaat

Reputation: 29234

You can use JSON.stringify, the third parameter is the number of spaces to indent.

JSON.stringify({name: "jason"}, null, 4)

results:

{
    "name": "jason"
}

Upvotes: 0

Related Questions