TestJson.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using LitJson;
  2. using Newtonsoft.Json;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. public class TestJson : MonoBehaviour
  7. {
  8. string message;
  9. // Start is called before the first frame update
  10. void Start()
  11. {
  12. // ToJson();
  13. StartCoroutine(StartText2());
  14. // StartText2();
  15. }
  16. // Update is called once per frame
  17. void Update()
  18. {
  19. }
  20. IEnumerator StartText2()
  21. {
  22. //读取streamingasset
  23. WWW www = new WWW(Application.streamingAssetsPath + "/co2_emission.json");
  24. yield return www;
  25. message = www.text;
  26. message = message.Replace(" ", "");
  27. Debug.Log(message);
  28. Dictionary<string, Dictionary<string, JData>> rotaDics = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, JData>>>(message);
  29. JRota rota = new JRota();
  30. message = message.Substring(1, message.Length - 2);//把message 最外面{} 去掉
  31. string[] str = message.Split(':');
  32. rota.time = str[0]; // 拿到时间
  33. for (int i = 0; i < message.Length; i++)
  34. {
  35. if(message[i] == ':')
  36. {
  37. message.Substring(i + 1, message.Length - 1); // 去除掉时间 : 前的字符
  38. }
  39. }
  40. Dictionary<string, JData> rotaDic = JsonConvert.DeserializeObject<Dictionary<string, JData>>(message);
  41. rota.rotaDic = rotaDic;
  42. foreach (var item in rotaDic)
  43. {
  44. Debug.Log(item.Value.AVIATION);
  45. }
  46. //if (message != "")
  47. //{
  48. // JsonData data = JsonMapper.ToObject(message);
  49. // Debug.Log(data["2020-01-01"]["UnitedArabEmirates"]["total_daily_emission"]);
  50. // Debug.Log(data["2020-01-01"][0]);
  51. // foreach (JsonData item in data["2020-01-01"][0])
  52. // {
  53. // Debug.Log(item["total_daily_emission"]);
  54. // }
  55. // //JRota Jrota = JsonMapper.ToObject<JRota>(message);
  56. // //Debug.Log(Jrota.listJdata.Count);
  57. //}
  58. }
  59. void ToJson()
  60. {
  61. Debug.Log(message);
  62. JsonData data = JsonMapper.ToObject(message);
  63. Debug.Log(data["2020-01-01"][0].ToString());
  64. //foreach (JData item in data["2020-01-01"])
  65. //{
  66. // Debug.Log
  67. //}
  68. }
  69. }
  70. public class JRota
  71. {
  72. public string time { get; set; }
  73. public Dictionary<string, JData> rotaDic { get; set; }
  74. }
  75. public class JData
  76. {
  77. public float total_daily_emission { get; set; }
  78. public float total_daily_emission_re { get; set; }
  79. public float CIlevel { get; set; }
  80. public float emission_per_second { get; set; }
  81. public float emission_per_second_re { get; set; }
  82. public float POWER_re { get; set; }
  83. public float POWER_pt { get; set; }
  84. public float POWER_re_pt { get; set; }
  85. public float INDUSTRY { get; set; }
  86. public float INDUSTRY_re{ get; set; }
  87. public float INDUSTRY_pt{ get; set; }
  88. public float INDUSTRY_re_pt{ get; set; }
  89. public float SURFACE_TRANSPORT{ get; set; }
  90. public float SURFACE_TRANSPORT_re{ get; set; }
  91. public float SURFACE_TRANSPORT_pt{ get; set; }
  92. public float SURFACE_TRANSPORT_re_pt{ get; set; }
  93. public float PUBLIC{ get; set; }
  94. public float PUBLIC_re{ get; set; }
  95. public float PUBLIC_pt{ get; set; }
  96. public float PUBLIC_re_pt{ get; set; }
  97. public float RESIDENTIAL{ get; set; }
  98. public float RESIDENTIAL_re{ get; set; }
  99. public float RESIDENTIAL_pt{ get; set; }
  100. public float RESIDENTIAL_re_pt{ get; set; }
  101. public float AVIATION{ get; set; }
  102. public float AVIATION_re{ get; set; }
  103. public float AVIATION_pt{ get; set; }
  104. public float AVIATION_re_pt{ get; set; }
  105. }
  106. public class Argentina : JData
  107. {
  108. }