C# dynamic 动态创建 json
1. 如何通过C# 的dynamic 创建如下json 对象?
{ "query": { "match": [{ "name": "jk", "age": "25" }, { "realName": "zs", "realAge": "9" }] }, "page": 5 }
2. 通过 dynamic + Dictionary<TKey,TValue> 即可实现 , 如下:
using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Dynamic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AnuoDog.Csharp { class StudyJsonNET { static void Main(string[] args) { 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); } } }
3.不知道还有木有其他更简单的创建方法?