1

I was weaving my own json programatically, and was adviced to not roll my own as escaping for the forward slash and line breaks won't be enough basically.

My json is to look like:

{"rc": "200", "m" : "", "o": "<div class='s1'>
            <div class='avatar'>                    
                <a href='\/asdf'>asdf<\/a><br \/>
                <strong>0<\/strong>
            <\/div>
            <div class='sl'>
                <p>
                    444444444
                <\/p>
            <\/div>
            <div class='clear'>
            <\/div>                        
        <\/div>"}

How can I use json.net library to create this? I don't want to create an object and serialize it as at this point I don't have the data in a class/object.

2 Answers 2

1

This will give you a object that you can continue to modify or just do a ToString on if all you want is the JSON text.

JObject o = new JObject();
o["rc"] = new JValue(200);
o["m"] = new JValue("");
o["o"] = new JValue(@"<div class='s1'>
      <div class='avatar'>             
          <a href='asdf'>asdf</a><br />
          <strong>0</strong>
      </div>
      <div class='sl'>
          <p>
              444444444
          </p>
      </div>
      <div class='clear'>
      </div>                        
  </div>");

Console.WriteLine(o.ToString());
Sign up to request clarification or add additional context in comments.

3 Comments

if you don't get rewarded an answer to this question who will? hehe thanks!
btw, is this the fastest way then?
1
JavaScriptConvert.SerializeObject(new {
    rc = 200,
    m = "",
    o = @"<div class='s1'>
            <div class='avatar'>                    
                <a href='/asdf'>asdf</a><br />
                <strong>0</strong>
            </div>
            <div class='sl'>
                <p>
                    444444444
                </p>
            </div>
            <div class='clear'>
            </div>                        
        </div>"
});

Or, if you need this as a controller result and you're using ASP.NET MVC you can just use the JsonResult ActionResult

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.