Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending HTML by Json

Tags:

json

c#

asp.net

Im responding this to a Jquery Ajax call and it works well:

var jSonTestResultReport =@"{ ""html"" : ""I am text!"" }";

The Javascript receives it and takes the text and presents it in a Div, i can see it saying "I am Text".

But when i try to do this:

var jSonTestResultReport =@"{ ""html"" : ""<li style=""color:green;"">I am text</li>"" }";

i get the error of

unexpected token c.

How can send html by Json?

like image 232
Daarwin Avatar asked Jan 25 '26 12:01

Daarwin


1 Answers

var jSonTestResultReport =@"{ ""html"" : ""<li style=""color:green;"">I am text</li>"" }";

will create a json string as

{ "html" : "<li style="color:green;">I am text</li>" }

which is not valid. unexpected token c. comes from the first letter of color.

Use a real json parser instead of forming json manually.

var json = JsonConvert.SerializeObject(new { html=@"<li style=""color:green;"">I am text</li>" });

PS: var jSonTestResultReport =@"{ ""html"" : "I am text!" }"; is not compilable.

like image 123
L.B Avatar answered Jan 28 '26 02:01

L.B