美文网首页
ExpandoObject 动态生成Json

ExpandoObject 动态生成Json

作者: code_搬运工 | 来源:发表于2021-12-01 13:43 被阅读0次

    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);
    

    相关文章

      网友评论

          本文标题:ExpandoObject 动态生成Json

          本文链接:https://www.haomeiwen.com/subject/stysxrtx.html