C# 生成 json 格式
"programmers":[{"firstName":"Brett","lastName":"McLaughlin","email":"aaaa"},{"firstNa...
"programmers": [
{ "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" },
{ "firstName": "Jason", "lastName":"Hunter", "email": "bbbb" },
{ "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" }
],
"authors": [
{ "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" },
{ "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" },
{ "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" }
],
"musicians": [
{ "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" },
{ "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" }
] }
我想生成这种格式的json 使用C#如何写 展开
{ "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" },
{ "firstName": "Jason", "lastName":"Hunter", "email": "bbbb" },
{ "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" }
],
"authors": [
{ "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" },
{ "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" },
{ "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" }
],
"musicians": [
{ "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" },
{ "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" }
] }
我想生成这种格式的json 使用C#如何写 展开
4个回答
展开全部
分成两部分:
1 类型定义部分,这部分可以手工定义,也可以采用工具定义,我个人使用vs2012的一个插件,可以选择性粘贴json为class,这个插件目前是测试版所以就不推荐了,你可以选择其他类似工具
public class Rootobject
{
public Programmer[] programmers { get; set; }
public Author[] authors { get; set; }
public Musician[] musicians { get; set; }
}
public class Programmer
{
public string firstName { get; set; }
public string lastName { get; set; }
public string email { get; set; }
}
public class Author
{
public string firstName { get; set; }
public string lastName { get; set; }
public string genre { get; set; }
}
public class Musician
{
public string firstName { get; set; }
public string lastName { get; set; }
public string instrument { get; set; }
}
第二部分,数据填充和转换工作,我这里演示过程,所以数据我随便填,你自己看着改
Rootobject b = new Rootobject()
{
programmers = new Programmer[]{
new Programmer(){ firstName="Brett", lastName="McLaughlin", email="aaaa"},
new Programmer(){ firstName="Jason", lastName="Hunter", email="bbbb"}
},
authors = new Author[]{
new Author(){ firstName="fa",lastName="la", genre="ga"},
new Author(){ firstName="fa1",lastName="la1",genre="ga1"}
},
musicians = new Musician[]{
new Musician(){ firstName="mm",lastName="lm",instrument="im"},
new Musician(){firstName="mm1",lastName="lm1",instrument="im1"}
}
};
JavaScriptSerializer oSerializer =
new System.Web.Script.Serialization.JavaScriptSerializer();
var json = oSerializer.Serialize(b);
//最后这json就是你要转换后的json字符串
1 类型定义部分,这部分可以手工定义,也可以采用工具定义,我个人使用vs2012的一个插件,可以选择性粘贴json为class,这个插件目前是测试版所以就不推荐了,你可以选择其他类似工具
public class Rootobject
{
public Programmer[] programmers { get; set; }
public Author[] authors { get; set; }
public Musician[] musicians { get; set; }
}
public class Programmer
{
public string firstName { get; set; }
public string lastName { get; set; }
public string email { get; set; }
}
public class Author
{
public string firstName { get; set; }
public string lastName { get; set; }
public string genre { get; set; }
}
public class Musician
{
public string firstName { get; set; }
public string lastName { get; set; }
public string instrument { get; set; }
}
第二部分,数据填充和转换工作,我这里演示过程,所以数据我随便填,你自己看着改
Rootobject b = new Rootobject()
{
programmers = new Programmer[]{
new Programmer(){ firstName="Brett", lastName="McLaughlin", email="aaaa"},
new Programmer(){ firstName="Jason", lastName="Hunter", email="bbbb"}
},
authors = new Author[]{
new Author(){ firstName="fa",lastName="la", genre="ga"},
new Author(){ firstName="fa1",lastName="la1",genre="ga1"}
},
musicians = new Musician[]{
new Musician(){ firstName="mm",lastName="lm",instrument="im"},
new Musician(){firstName="mm1",lastName="lm1",instrument="im1"}
}
};
JavaScriptSerializer oSerializer =
new System.Web.Script.Serialization.JavaScriptSerializer();
var json = oSerializer.Serialize(b);
//最后这json就是你要转换后的json字符串
展开全部
如果你想简单些的话,网上有现成;如果你不想发布时附随dll的话就要自己写的了...
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
利用 Newtonsoft.Json.dll 自己去百度一下,简单到我不想解释
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询