c# - Characters not escaped properly when passing to dictionary -


before marked duplicate, other question similar name pertinent regex , different problem.

i have string

principal = "{\"id\":\"number\"}" 

if not mistaken should escape {"id":"number"}.

however, when pass following method

dictionary<string, object> folder = new dictionary<string, object>();             folder.add("principal", principal);             string json = jsonconvert.serializeobject(folder);             console.writeline(json); 

it returns

{ "principal":"{\"id\":\"number\"}" } 

ideally return

{ "principal":{"id":"number"} } 

why holding on quotes , escape characters? doing wrong here?

your principal string, , gets escaped one.

if want escape json object, needs object in application well.

if want deserialize or use more once, propose define object in class. if not, can use anonymous object :

dictionary<string, object> folder = new dictionary<string, object>(); folder.add("principal", new { id = "number" }); string json = jsonconvert.serializeobject(folder); console.writeline(json); 

/edit: , here non-anonymous class:

class definition:

class principal {     public string id { get; set; } } 

usage:

dictionary<string, object> folder = new dictionary<string, object>(); folder.add("principal", new principal(){ id = "number" }); string json = jsonconvert.serializeobject(folder); console.writeline(json); 

Comments