ExpandoObject 动态生成Json

2021-12-01  本文已影响0人  code_搬运工

key固定

            dynamic objmatch = new ExpandoObject();
 
            Dictionary<string, string> matchKV = new Dictionary<string, string>();
            matchKV.Add("name", "jk");
            matchKV.Add("age","25");
 
            Dictionary<string, string> matchKV2 = new Dictionary<string, string>();
            matchKV2.Add("realName", "zs");
            matchKV2.Add("realAge", "9");
 
            List<dynamic> matchList = new List<dynamic>();
            matchList.Add(matchKV);
            matchList.Add(matchKV2);
 
 
            objmatch.match = matchList;
 
            dynamic obj = new ExpandoObject();
            obj.query = objmatch;
            obj.page = 5;
 
            string json = JsonConvert.SerializeObject(obj);

key不确定


            dynamic obj = new ExpandoObject();
            Dictionary<string, object> JsonProperties = new Dictionary<string, object>();
            JsonProperties.Add("Name", "张三");
            JsonProperties.Add("Sex", "男");
            JsonProperties.Add("Age", 12);
            List<dynamic> ParaArr = new List<dynamic>();
            Dictionary<string, object> bookProperties = new Dictionary<string, object>();
            bookProperties.Add("Name","语文");
            bookProperties.Add("Prices",15);
            ParaArr.Add(bookProperties);
            JsonProperties.Add("课程", ParaArr);


            obj = JsonProperties;

            //obj.query = objmatch;
            //obj.page = 5;

            //string Name = "Name";
            //string age = "age";
            //obj[Name] = "111";
            //obj[age] = 1;

            //obj.Name

            //obj = ParaArr;

            string json = JsonConvert.SerializeObject(obj);
上一篇下一篇

猜你喜欢

热点阅读