1

I have the following text:

Hello <b>User</p>

I am storing it as JSOn string in the database so as to use it later to deserialize it. Below is the JSON string sent to DB in MyProperty column:

{"Html":"Hello <b>User</p>"}

In order to deserialize that string back to my object I am using NewtonSoft.JSON dll.

JsonConvert.DeserializeObject<MyClass>(this.MyProperty);

When I deserialize it I am unable to get "Hello <b>User</p>" value back, it comes as null.

Note: If I am not string HTML in the JSON string it is returned back while deserializing.

Is there any limitation with html tags in JSON string?

My goal is store html tags in JSON string and get it back.

Edit:

Code of MyClass:

 public class MyClass
    {
        public string Html { get; set; }
        public string MyProperty { get; set; }
    }
6
  • 2
    Could you also include what MyClass looks like? Commented Nov 20, 2015 at 14:34
  • As I mentioned if there are no html tags in the string i.e plain text it is deserialized back. Commented Nov 20, 2015 at 14:39
  • Is this.MyProperty a string? I tried this by making the json object a string and it deserializes just fine. I'm using .Net 4.5 though, not sure if that makes a difference. Commented Nov 20, 2015 at 14:44
  • Works fine for me. Please edit your question to include an MCVE that demonstrates the problem. Commented Nov 20, 2015 at 14:47
  • JSON.org says there is no escaping for opening and closing tags, only for backslash and quote and control chars. Thus the JSON string is valid. I tried that string with an online Jason parser and it is parsed correctly. Problem must be somewhere else Commented Nov 20, 2015 at 14:50

1 Answer 1

1

This works:

static void Main(string[] args)
{
    string json = "{\"Html\":\"Hello<b>User</p>\" }";
    var myClass = JsonConvert.DeserializeObject<MyClass>(json);
}

public class MyClass
{
    public string Html { get; set; }
    public string MyProperty { get; set; }
}
Sign up to request clarification or add additional context in comments.

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.