之前我遇到过这个问题,在那里我创建了一个数据模型,稍后将序列化为JSON字符串,但我希望包含属性的类也被序列化。请参阅以下示例:
我有我的数据模型:
public class MyModel
{
[JsonProperty(PropertyName = "Prop1")]
public string Property1 { get; set; }
[JsonProperty(PropertyName = "Prop2")]
public string Property2 { get; set; }
}
然后将序列化为:
{
"Prop1":"Some Value",
"Prop2":"Some Value"
}
有没有办法让它序列化为:
{
"MyModel":
{
"Prop1":"Some Value",
"Prop2":"Some Value"
}
}
我目前正在做的事情似乎不适合为我的JSON创建一个包装对象:
string object = @"{""ticket"":" + JsonConvert.SerializeObject(model) + @"}"
我可以在课堂上添加某种属性,例如:
[SerializeThisClass, ProperName="MyModel"]
public class MyModel
{
[JsonProperty(PropertyName = "Prop1")]
public string Property1 { get; set; }
[JsonProperty(PropertyName = "Prop2")]
public string Property2 { get; set; }
}
答案 0 :(得分:10)
<iframe id="vt" width="420" autoplay="false" height="345" src="http://cache4.asset-cache.net/xd/468140002.mp4?v=1&c=IWSAsset&k=2&d=72990B68305E4FDFEE95B69A879131BCD6D7111452E48A17C03F8342D660D5A451EBC1DEC2A827C9&b=RTdG" frameborder="0" allowfullscreen></iframe>
jQuery(document).ready(function () {
// var url = $('#vt').attr('src');
// $('#vt').attr('src', '');
// $('#vt').attr('src', url);
var vid = document.getElementById("vt");
vid.pause()
//vid.onpause = function() {
//alert("The video has been paused");
};
});
应该没问题
答案 1 :(得分:1)
添加另一个以MyModel为成员的类,然后序列化父。
public class Parent
{
[JsonProperty(PropertyName = "MyModel")]
public MyModel MyModel { get; set; }
}
答案 2 :(得分:0)
您可以通过此
实现这一目标 public class MyModel
{
[JsonProperty(PropertyName = "Prop1")]
public string Property1 { get; set; }
[JsonProperty(PropertyName = "Prop2")]
public string Property2 { get; set; }
}
public class Wrapper{
[JsonProperty(PropertyName = "MyModel")]
public MyModel myModel{get;set;}
}
然后序列化Wrapper对象