Ashutosh
Ashutosh

Reputation: 201

How do I send data which contains a double-quote using JSON?

I have a problem with JSON if our data has double quotes like this: " 15" " (15 inch).

JSON is not parsing it.

My code looks like this:

    String strheader = convert.ToString(dt.table[0].rows[0]["Size"]);
    Response.ContentType = "application/json; charset=utf-8";
    Response.Write(strheader);

How can I solve this?

Upvotes: 0

Views: 1515

Answers (4)

Carlos Muñoz
Carlos Muñoz

Reputation: 17804

Use a Json library like Json.NET or Simple Json for the task since them will escape your data automaticaly.

If it is just a simple task then just escape it manually like some have suggested

Upvotes: 1

VAShhh
VAShhh

Reputation: 3504

Give a look at the class DataContractJsonSerializer

Upvotes: 0

Oded
Oded

Reputation: 498972

Just escape the inner " by adding a \ before it:

" 15\" " 

Upvotes: 1

Layke
Layke

Reputation: 53146

You need to escape it.

 { book: "How to code C++ in 24 hour steps", author: "O\"Reilly" }

Upvotes: 4

Related Questions