如何输出一下的 json 格式~

2014-11-22 14:23:37 +08:00
 xarrow
<pre>
{
"name": "lisi",
"children": [

{
"name": "mail",
"children": [
{"name": "lisi@gmail.com"}
]
},
{
"name": "password",
"children": [
{"name": "lisi"}
]
}
]
}

</pre>

------------------------------------------------------------------------------------
Json.NET下只能输出
[
{ "username": "zhangsan1981",
"password":"123456",
"mail": "zhuwh1981@gmail.com"
}
]
2543 次点击
所在节点    问与答
15 条回复
realityone
2014-11-22 20:18:10 +08:00
children那里不是一个array吗。。
xarrow
2014-11-22 20:38:57 +08:00
@realityone 怎么写?
realityone
2014-11-22 22:43:49 +08:00
@xarrow .net不熟。。
就是children那里赋一个对应的list给他啊,list里面好像还有dic。。
xarrow
2014-11-22 22:47:04 +08:00
@realityone 正在考虑用Python~
realityone
2014-11-22 22:53:09 +08:00
@xarrow
py的话。。
{'name': 'lisi', 'children': [{'name': 'mail', 'children': [{'name': 'lisi@gmail.com'}]}, {'name': 'password', 'children': [{'name': 'lisi'}]}]}
mkeith
2014-11-23 00:42:36 +08:00
楼主要干什么啊?
cdxem713
2014-11-23 01:50:18 +08:00
json.net的话,简单的就是一个dictinary<string,object>(); children那个键值用list<dictinary<string,object>>();最后JSON.serializeObject就可以了
xarrow
2014-11-23 10:46:19 +08:00
@cdxem713 C#新手,我还是不太明白~
cdxem713
2014-11-23 14:21:55 +08:00
@xarrow

var mainJson = new Dictionary<string, object>();
mainJson.Add("name","lisi");
var mainChilds = new List<Dictionary<string, object>>();

var sub1 = new Dictionary<string, object>();
sub1.Add("name","mail");
var sub1Childs = new List<Dictionary<string, object>>();
sub1Childs.Add(new Dictionary<string, object> { { "name", "lisi@gmail.com" } });
sub1.Add("children",sub1Childs);

var sub2 = new Dictionary<string, object>();
sub2.Add("name", "password");
var sub2Childs = new List<Dictionary<string, object>>();
sub2Childs.Add(new Dictionary<string, object> { { "name", "lisi" } });
sub2.Add("children",sub2Childs);

mainChilds.Add(sub1);
mainChilds.Add(sub2);

mainJson.Add("children",mainChilds);

Console.WriteLine(JsonConvert.SerializeObject(mainJson));
Console.ReadLine();
xarrow
2014-11-23 22:39:18 +08:00
@cdxem713 臣妾真的不懂json啊!!啥玩意儿这是.....
大牛,跪求完整处理代码~~

{
"name": "key_word",

"children": [
{
"name": "zhangsan",
"children": [

{
"name": "mail",
"children": [
{"name": "zhangsan@gmail.com"}
]
},
{
"name": "password",
"children": [
{"name": "12345678"}
]
}
]
},
{
"name": "lisi",
"children": [

{
"name": "mail",
"children": [
{"name": "lisi@gmail.com"}
]
},
{
"name": "password",
"children": [
{"name": "lisi"}
]
}
]
}


]
}

//////////////////////////////////////////////////////////////
结构
|------mail
|-----zhangsan-|
| |------password
key_word----| |------mail
|-----lisi-|
| |------password
|-----wangwu
|
|-----.....
xarrow
2014-11-23 22:46:02 +08:00
cdxem713
2014-11-23 22:52:44 +08:00
@xarrow 额,其实那代码真是可用的。。。我运行过
cdxem713
2014-11-23 23:03:51 +08:00
@xarrow GOD,你这JSON好奇葩,如果是自己造的格式,最好还是改下,像mail,password这种都可以用作键的,你给我个邮件地址,我等下写了传给你吧
cdxem713
2014-11-23 23:08:53 +08:00
@xarrow
{
"name":"key_word",
"children":[
{
"name":"zhangsan",
"mail":"zhangsan@gmail.com",
"password":"12345678"
},
{
"name":"lisi",
"mail":"lisi@gmail.com",
"password":"12345678"
}
]
}

这个格式我觉得会好些
xarrow
2014-11-23 23:48:35 +08:00
@cdxem713
d3js中的flare.json格式,就一个小小的json晕了我一下午,晕死~
上面代码可以运行的,我想修改的,但又不理解,烦请大牛处理下~

zhjt111#gmail.com
十分感谢!>_<

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/148502

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX