JsonUtility.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using SimpleJSON;
  2. using UnityEngine;
  3. using System;
  4. namespace WJ{
  5. public class JsonUtility{
  6. public static Vector3 GetVec(JSONArray json ){
  7. return new Vector3(json[0].AsFloat, json[1].AsFloat, json[2].AsFloat);
  8. }
  9. public static JSONArray GetVecJson(Vector3 vec ){
  10. JSONArray result = new JSONArray();
  11. result.Add(new JSONData(vec.x) );
  12. result.Add(new JSONData(vec.y) );
  13. result.Add(new JSONData(vec.z) );
  14. return result;
  15. }
  16. public static Vector2Int GetVecFromJson(JSONNode vecJson ){
  17. JSONArray vecArr = vecJson.AsArray;
  18. return new Vector2Int(
  19. vecArr[0].AsInt,
  20. vecArr[1].AsInt
  21. );
  22. }
  23. public static Vector3 GetVecFromStr(string vecStr ){
  24. string[] tempStrs = vecStr.Split(',');
  25. return new Vector3(
  26. float.Parse(tempStrs[0]),
  27. float.Parse(tempStrs[1]),
  28. float.Parse(tempStrs[2])
  29. );
  30. }
  31. public static JSONArray GetArray(string arrayStr ){
  32. JSONArray result = JSONArray.Parse(arrayStr).AsArray;
  33. return result;
  34. }
  35. public static float GetFloatValue(JSONNode json, float defaultValue ){
  36. if (string.IsNullOrEmpty(json.ToString()) ){
  37. return defaultValue;
  38. }
  39. return json.AsFloat;
  40. }
  41. public static long GetLongValue(JSONNode json, long defaultValue ){
  42. if (string.IsNullOrEmpty(json.ToString()) ){
  43. return defaultValue;
  44. }
  45. return json.AsLong;
  46. }
  47. public static void SetNodeIfNoEmpty(
  48. JSONNode json, string key, string value, string defaultValue = "0"){
  49. if (value != defaultValue ){
  50. json[key] = value;
  51. }
  52. }
  53. public static string UnEscapeJavascriptString(string jsonString){
  54. if (String.IsNullOrEmpty(jsonString))
  55. return jsonString;
  56. System.Text.StringBuilder sb = new System.Text.StringBuilder();
  57. char c;
  58. for (int i = 0; i < jsonString.Length; )
  59. {
  60. c = jsonString[i++];
  61. if (c == '\\')
  62. {
  63. int remainingLength = jsonString.Length - i;
  64. if (remainingLength >= 2)
  65. {
  66. char lookahead = jsonString[i];
  67. if (lookahead == '\\')
  68. {
  69. sb.Append('\\');
  70. ++i;
  71. }
  72. else if (lookahead == '"')
  73. {
  74. sb.Append("\"");
  75. ++i;
  76. }
  77. else if (lookahead == 't')
  78. {
  79. sb.Append('\t');
  80. ++i;
  81. }
  82. else if (lookahead == 'b')
  83. {
  84. sb.Append('\b');
  85. ++i;
  86. }
  87. else if (lookahead == 'n')
  88. {
  89. sb.Append('\n');
  90. ++i;
  91. }
  92. else if (lookahead == 'r')
  93. {
  94. sb.Append('\r');
  95. ++i;
  96. }else if (lookahead == 'u'){
  97. char[] hex = new char[4];
  98. for (int m=0; m< 4; m++) {
  99. hex[m] = jsonString[i+m+1];
  100. }
  101. sb.Append((char) Convert.ToInt32(new string(hex), 16));
  102. i++;
  103. i += 4;
  104. }
  105. }
  106. }
  107. else
  108. {
  109. sb.Append(c);
  110. }
  111. }
  112. //Debug.Log(sb.ToString() );
  113. return sb.ToString();
  114. }
  115. }}