• 全部
  • C#综合技术
  • C#互联网桌面应用
  • AppLauncher
  • WinForm&WPF
  • C#开发新技术
  • 问答

大家好,我怎么把一个对象序列化到这样一个JSON格式

listhome 2014-03-07 07:07:57
类定义于如下

public abstract class WXResponse
{
[DataMember(Name="touser")]
public String ToUserName
{
get;
set;
}

public String FromUserName
{
get;
set;
}

public DateTime CreateTime
{
get;
set;
}

}


[DataContract]
public class ArticleResponse:WXResponse
{
private List<ArticleItem> Articles = new List<ArticleItem>();

public class ArticleItem
{
[DataMember(Name = "title")]
public String Title
{
get;
set;
}
[DataMember(Name = "description")]
public String Description
{
get;
set;
}
[DataMember(Name = "picurl")]
public String PicUrl
{
get;
set;
}
[DataMember(Name = "url")]
public String Url
{
get;
set;
}
}

public int ArticleCount
{
get
{
return Articles.Count;
}
}

public void AddItem(String title,
String Description,
String PicUrl,
String url)
{
Articles.Add(new ArticleItem() {
Title = title,
Description = Description,
PicUrl = PicUrl,
Url = url,
});
}

[DataMember(Name="msgtype")]
public String MsgType
{
get {
return "news";
}
set {

}

}
[DataMember(Name="articles" )]
public List<ArticleItem> News
{
get {
return Articles;
}
}

}

{
"touser":"OPENID",
"msgtype":"news",
"news":{
"articles": [
{
"title":"Happy Day",
"description":"Is Really A Happy Day",
"url":"URL",
"picurl":"PIC_URL"
},
{
"title":"Happy Day",
"description":"Is Really A Happy Day",
"url":"URL",
"picurl":"PIC_URL"
}
]
}
}

关键是那个news 下的 Article
...全文
76 点赞 收藏 1
写回复
1 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
xu56180825 2014-03-07
牛顿牌JSON,听说过吗?JSON.NET
回复
发帖
C#
创建于2007-09-28

10.5w+

社区成员

.NET技术 C#
申请成为版主
帖子事件
创建了帖子
2014-03-07 07:07
社区公告

让您成为最强悍的C#开发者