{
"Model": [{
"a": "111",
"b": "222",
"c": {
"c1": "c111"
},
"d": {
"d1": "d111"
},
"entity": [{
"a": "111",
"b": "222",
"c": {
"c1": "c111"
}
}, {
"a": "111",
"b": "222",
"c": {
"c1": "c111"
}
}]
}]
}
不用拼接的方式,如何生成这样格式类型的json?
你上边写的json格式不变了吧?如果是固定格式,直接定义一个对应的model吧。我看你里边有的有d,有的没有。。。
求一个能实现上面json格式的demo
@EarieLee: public class Models
{
public string a { get; set; }
public string b { get; set; }
public c c = new c();
public d d = new d();
public List<entity> entity = new List<entity>();
}
public class entity:c
{
public string a { get; set; }
public string b { get; set; }
}
public class c
{
public string c1 { get; set; }
}
public class d
{
public string d1 { get; set; }
}
@一禅·小和尚: 输出是这样的
{"a":null,"b":null,"c":{"c1":null},"d":{"d1":null},"entity":[{"a":"","b":"","c1":""},{"a":"","b":"","c1":""}]}
@一禅·小和尚: 序列化,字符串优先级默认在后面的,需要打标识才能按规定排序
@EarieLee: 是的。你可以根据实际情况更正一下。
@EarieLee: 对了,我记得有个貌似Order的标识,你是用的哪个?
@一禅·小和尚: JsonProperty
@EarieLee: 这个没用过,发一个demo出来研究一下。
对象转json
json.parse
C#用这个 Newtonsoft.Json
类与json转换, 现在各种语言都有
序列化与反序列化 了解一下
/* 目标格式
* {
"1": "",
"2": {
"2.1": ""
},
"3": {
"3.1": "",
"3.2": ""
},
"4": ""
}
*/
//这边假设是数组 新增测试数据
Dictionary<string, Dictionary<string, string>> testData = new Dictionary<string, Dictionary<string, string>>();
Dictionary<string, string> t1 = new Dictionary<string, string>();
testData.Add("1", new Dictionary<string, string>());
Dictionary<string, string> t2 = new Dictionary<string, string>();
t2.Add("2.1", "");
testData.Add("2", t2);
Dictionary<string, string> t3 = new Dictionary<string, string>();
t3.Add("3.1", "");
t3.Add("3.2", "");
testData.Add("3", t3);
Dictionary<string, string> t4 = new Dictionary<string, string>();
testData.Add("4", t4);
//json化
var json = Newtonsoft.Json.JsonConvert.SerializeObject(testData).ToString();
{
"Model": [{
"a": "111",
"b": "222",
"c": {
"c1": "c111"
},
"d": {
"d1": "d111"
},
"entity": [{
"a": "111",
"b": "222",
"c": {
"c1": "c111"
}
}, {
"a": "111",
"b": "222",
"c": {
"c1": "c111"
}
}]
}]
}
请问这种格式的要怎么生成转换比较好
在线有很多json格式话工具,其实各大浏览器也都带有json格式化工具