首页 新闻 会员 周边

数据转换成json格式

0
悬赏园豆:60 [已解决问题] 解决于 2019-03-28 11:56

{
"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?

EarieLee的主页 EarieLee | 初学一级 | 园豆:148
提问于:2019-03-25 15:48
< >
分享
最佳答案
1

你上边写的json格式不变了吧?如果是固定格式,直接定义一个对应的model吧。我看你里边有的有d,有的没有。。。

收获园豆:45
一禅·小和尚 | 小虾三级 |园豆:519 | 2019-03-26 15:44

求一个能实现上面json格式的demo

EarieLee | 园豆:148 (初学一级) | 2019-03-26 15:46

@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; }
    }
一禅·小和尚 | 园豆:519 (小虾三级) | 2019-03-27 13:51

@一禅·小和尚: 输出是这样的
{"a":null,"b":null,"c":{"c1":null},"d":{"d1":null},"entity":[{"a":"","b":"","c1":""},{"a":"","b":"","c1":""}]}

一禅·小和尚 | 园豆:519 (小虾三级) | 2019-03-27 13:52

@一禅·小和尚: 序列化,字符串优先级默认在后面的,需要打标识才能按规定排序

EarieLee | 园豆:148 (初学一级) | 2019-03-28 15:41

@EarieLee: 是的。你可以根据实际情况更正一下。

一禅·小和尚 | 园豆:519 (小虾三级) | 2019-03-29 12:39

@EarieLee: 对了,我记得有个貌似Order的标识,你是用的哪个?

一禅·小和尚 | 园豆:519 (小虾三级) | 2019-03-29 15:10

@一禅·小和尚: JsonProperty

EarieLee | 园豆:148 (初学一级) | 2019-03-29 15:12

@EarieLee: 这个没用过,发一个demo出来研究一下。

一禅·小和尚 | 园豆:519 (小虾三级) | 2019-03-29 15:13
其他回答(7)
0

对象转json

ifz | 园豆:302 (菜鸟二级) | 2019-03-25 15:55
0

json.parse

地火水 | 园豆:1290 (小虾三级) | 2019-03-25 16:34
0

C#用这个 Newtonsoft.Json

jqw2009 | 园豆:2439 (老鸟四级) | 2019-03-25 16:41
0

类与json转换, 现在各种语言都有

风浪 | 园豆:2996 (老鸟四级) | 2019-03-25 16:43
0

序列化与反序列化 了解一下

雷。 | 园豆:775 (小虾三级) | 2019-03-25 17:14
1

/* 目标格式
* {
"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();
收获园豆:15
三人乐乐 | 园豆:4819 (老鸟四级) | 2019-03-26 11:04

{
"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"
}
}]
}]
}

请问这种格式的要怎么生成转换比较好

支持(0) 反对(0) EarieLee | 园豆:148 (初学一级) | 2019-03-26 13:27
0

在线有很多json格式话工具,其实各大浏览器也都带有json格式化工具

jerry-Tom | 园豆:4077 (老鸟四级) | 2019-03-26 17:37
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册