asp.net json序列化器问题

时间:2011-06-16 03:45:39

标签: c# asp.net json

我有一个看起来像这样的对象:

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;}

}

此对象包含9个列表和9个字符串,用于jsonformat和完整对象的ObjectInJson字符串中的每个列表。

然后,我有一个MyObject列表。

我正在使用JavascriptSerializer:我首先为每个对象序列化每个列表,并将相应的字符串存储在InJson变量中。然后,我通过仅包含列表的int,byte和json字符串来序列化每个MyObject。最后,我序列化了MyObject列表。

我最终得到了一个包含很多\\\“的字符串,我想知道是否有人遇到过.net json序列化程序的问题。

[{"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 Address\\\",\\\"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,

感谢。

2 个答案:

答案 0 :(得分:1)

您只需要序列化MyObject的实例。 Serializer会相应地自动序列化内部列表。

如果您需要忽略任何属性,请使用像russau建议的[ScriptIgnore]属性。

例如

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

答案 1 :(得分:0)

你说你是“三重JSON序列化”的东西,然后你可能会得到所有引用。当您将任何对象转换为JSON时,您将获得一个字符串,该字符串在obejct名称(可能是值)周围包含引号(“)。如果再次序列化,则字符串内的引号需要转义,因此{{1如果你再次序列化它,那么转义标志本身也将被转义,因此\"