开发者

asp.net json serializer issue

开发者 https://www.devze.com 2023-03-13 13:05 出处:网络
I have an object that looks like this: public MyObject { public int ObjectID {get;set;} public byte ObjectStatus {get;set;}

I have an object that looks like this:

public MyObject
{
  public int ObjectID {get;set;}
  public byte ObjectStatus {get;set;}
  public List<MySubObject1> {get;set}
  public List<MySubObject2> {get;set}
  ...
  public List<MySubObject9> {get;set}

  public string MySubObject1InJson {get;set;}
  ...
  public string MySubObject9InJson {get;set;}

  public string ObjectInJson {get;set;}

}

This object contains 9 lists and 9 strings for each list in jsonformat and ObjectInJson string for the full object.

And then, I have a list of MyObject.

I'm using JavascriptSerializer: I first serialize each list for each object and store the corresponding string on the InJson variable. Then, I serialize each MyObject by including only the int, byte and json strings of lists. Finally, I serialize the list of MyObject.

I end up with a string that has a lot of \\\" in it and I was wondering if someone had come across such problem with the .net json serializer.

[{"ObjectID":1492,"ObjectStatus":2,"ObjectJsonFormat":"{\"ObjectID\":1492,\"LStatus\":2,\"LAppoints\":\"[{\\\"AppointID\\\":23350,\\\"AppointType\\\":2,\\\"AppointStatus\\\":3,\\\"AppointDate\\\":\\\"4/23/2011\\\",\\\"AppointTime\\\":\\\"6:49 PM\\\",\\\"AppointSetDate\\\":\\\"4/23/2011\\\",\\\"AppointSetTime\\\":\\\"6:49 PM\\\",\\\"AppointResolvDate\\\":\\\"4/28/2011\\\",\\\"AppointResolvTime\\\":\\\"6:49 PM\\\",\\\"AppointFirst\\\":\\\"NATHAN\\\",\\\"AppointLast\\\":\\\"WEST\\\",\\\"ContactFirstLastListing\\\":\\\"NATHAN WEST\\\"},{\\\"AppointID\\\":23351,\\\"AppointType\\\":1,\\\"AppointStatus\\\":2,\\\"AppointDate\\\":\\\"4/23/2011\\\",\\\"AppointTime\\\":\\\"8:19 PM\\\",\\\"AppointSetDate\\\":\\\"4/23/2011\\\",\\\"AppointSetTime\\\":\\\"8:19 PM\\\",\\\"AppointResolvDate\\\":\\\"4/28/2011\\\",\\\"AppointResolvTime\\\":\\\"8:19 PM\\\",\\\"AppointFirst\\\":\\\"DALE\\\",\\\"AppointLast\\\":\\\"JORDAN\\\",\\\"ContactFirstLastListing\\\":\\\"DALE JORDAN\\\"},{\\\"AppointID\\\":23352,\\\"AppointType\\\":1,\\\"AppointStatus\\\":3,\\\"AppointDate\\\":\\\"4/23/2011\\\",\\\"AppointTime\\\":\\\"9:49 PM\\\",\\\"AppointSetDate\\\":\\\"4/23/2011\\\",\\\"AppointSetTime\\\":\\\"9:49 PM\\\",\\\"AppointResolvDate\\\":\\\"4/28/2011\\\",\\\"AppointResolvTime\\\":\\\"9:49 PM\\\",\\\"AppointFirst\\\":\\\"MANUEL\\\",\\\"AppointLast\\\":\\\"WOODS\\\",\\\"ContactFirstLastListing\\\":\\\"MANUEL WOODS\\\"}]\",\"LNotes\":\"[{\\\"NoteID\\\":14201,\\\"NoteText\\\":\\\"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostr\\\",\\\"NoteDate\\\":\\\"1/10/2011\\\",\\\"NoteTime\\\":\\\"11:54 AM\\\"},{\\\"NoteID\\\":14202,\\\"NoteText\\\":\\\"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostr\\\",\\\"NoteDate\\\":\\\"1/10/2011\\\",\\\"NoteTime\\\":\\\"12:24 PM\\\"},{\\\"NoteID\\\":14203,\\\"NoteText\\\":\\\"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostr\\\",\\\"NoteDate\\\":\\\"1/10/2011\\\",\\\"NoteTime\\\":\\\"12:54 PM\\\"},{\\\"NoteID\\\":14204,\\\"NoteText\\\":\\\"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostr\\\",\\\"NoteDate\\\":\\\"1/10/2011\\\",\\\"NoteTime\\\":\\\"1:24 PM\\\"}]\",\"LCustom\":\"0\",\"LContact\":\"[{\\\"ContactID\\\":8758,\\\"ContactSort\\\":1,\\\"ContactFirst\\\":\\\"STEVEN\\\",\\\"ContactMiddle\\\":\\\"\\\",\\\"ContactLast\\\":\\\"GARCIA\\\",\\\"ContactFirstLast\\\":\\\"STEVEN GARCIA\\\",\\\"ContactEmail\\\":\\\"steven@garcia.com\\\"},{\\\"ContactID\\\":8759,\\\"ContactSort\\\":2,\\\"ContactFirst\\\":\\\"EDWARD\\\",\\\"ContactMiddle\\\":\\\"\\\",\\\"ContactLast\\\":\\\"MARTINEZ\\\",\\\"ContactFirstLast\\\":\\\"EDWARD MARTINEZ\\\",\\\"ContactEmail\\\":\\\"edward@martinez.com\\\"},{\\\"ContactID\\\":8760,\\\"ContactSort\\\":3,\\\"ContactFirst\\\":\\\"BRIAN\\\",\\\"ContactMiddle\\\":\\\"\\\",\\\"ContactLast\\\":\\\"ROBINSON\\\",\\\"ContactFirstLast\\\":\\\"BRIAN ROBINSON\\\",\\\"ContactEmail\\\":\\\"brian@robinson.com\\\"}]\",\"LAddress\":\"[{\\\"AddressID\\\":14293,\\\"AddressSort\\\":1,\\\"AddressLabel\\\":\\\"Primary Addres开发者_运维问答s\\\",\\\"Address1\\\":\\\"1305 S 1st St\\\",\\\"Address2\\\":\\\"Suite 3\\\",\\\"AddressCity\\\":\\\"San Jose\\\",\\\"AddressState\\\":\\\"CA\\\",\\\"AddressZip\\\":\\\"95008\\\",\\\"AddressCounty\\\":\\\"Santa Clara\\\",\\\"AddressCountry\\\":\\\"USA\\\"}]\",\"LBiz\":\"0\",\"LPhone\":\"0\",\"LStatusH\":\"[{\\\"NewStatus\\\":2,\\\"StatusDate\\\":\\\"4/09/2011\\\"},{\\\"NewStatus\\\":3,\\\"StatusDate\\\":\\\"4/14/2011\\\"},{\\\"NewStatus\\\":2,\\\"StatusDate\\\":\\\"4/19/2011\\\"}]\",\"LEmail\":\"0\",\"LCall\":\"[{\\\"CallDate\\\":\\\"5:54 AM\\\",\\\"CallTime\\\":\\\"5/20/2011\\\",\\\"CallDuration\\\":5457,\\\"CallOutcome\\\":1,\\\"CallPhonePrefix\\\":1,\\\"CallPhoneNumber\\\":\\\"4084840241\\\"},{\\\"CallDate\\\":\\\"5:24 AM\\\",\\\"CallTime\\\":\\\"5/23/2011\\\",\\\"CallDuration\\\":11,\\\"CallOutcome\\\":4,\\\"CallPhonePrefix\\\":1,\\\"CallPhoneNumber\\\":\\\"4084840241\\\"}]\"}"},{"ObjectID":1496,

Thanks.


You just need to serialize the instance of MyObject. Serializer will automatically serialize the inner lists accordingly.

If you need to ignore any property use the [ScriptIgnore] atribute like russau suggested.

eg.

public class MySubObject
{
 // The JavaScriptSerializer ignores this field.
        [ScriptIgnore]
        public int useID { get; set; }
}


You say that you're "triple-JSON-serializing" something, then you'll likely end up with all the quotes. When you convert any object to JSON, you'll get a string which contains quotes (") around the obejct names (and possibly values). If you serialize it again, the quotes inside strings need to be escaped, thus the \". And if you serialize it again, then the escape sign itself is going to be escaped as well, thus the \\\".

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号