C# dynamic 動態創建 json

  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Dynamic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace AnuoDog.Csharp
  10. {
  11. class StudyJsonNET
  12. {
  13. static void Main(string[] args)
  14. {
  15. dynamic objmatch = new ExpandoObject();
  16.  
  17. Dictionary<string, string> matchKV = new Dictionary<string, string>();
  18. matchKV.Add("name", "jk");
  19. matchKV.Add("age","25");
  20.  
  21. Dictionary<string, string> matchKV2 = new Dictionary<string, string>();
  22. matchKV2.Add("realName", "zs");
  23. matchKV2.Add("realAge", "9");
  24.  
  25. List<dynamic> matchList = new List<dynamic>();
  26. matchList.Add(matchKV);
  27. matchList.Add(matchKV2);
  28.  
  29. objmatch.match = matchList;
  30.  
  31. dynamic obj = new ExpandoObject();
  32. obj.query = objmatch;
  33. obj.page = 5;
  34.  
  35. string json = JsonConvert.SerializeObject(obj);
  36. }
  37. }
  38. }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章