前端转vue
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JsonHelper.ts 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // 模拟 PlatformLogger.SystemInfo 方法,实际使用时需要替换为真实实现
  2. const PlatformLogger = {
  3. SystemInfo: (message: string) => {
  4. console.log(message);
  5. }
  6. };
  7. /**
  8. * 将json串转换成Dictionary对象
  9. * @param jsonString
  10. * @returns
  11. */
  12. export function ParseJson2Dictionary(jsonString: string): { [key: string]: any } {
  13. const json = JSON.parse(jsonString);
  14. return ParseJObject2Dictionary(json);
  15. }
  16. /**
  17. * 将json串转换成Dictionary对象,深度为1
  18. * @param jsonString
  19. * @returns
  20. */
  21. export function ParseJson2StringDictionary(jsonString: string): { [key: string]: string } {
  22. const json = JSON.parse(jsonString);
  23. const dict = ParseJObject2Dictionary(json, true);
  24. const result: { [key: string]: string } = {};
  25. for (const key in dict) {
  26. if (dict.hasOwnProperty(key)) {
  27. result[key] = String(dict[key]);
  28. }
  29. }
  30. return result;
  31. }
  32. /**
  33. * 将json串转换成List对象
  34. * @param jsonString
  35. * @returns
  36. */
  37. export function ParseJson2List(jsonString: string): any[] {
  38. const json = JSON.parse(jsonString);
  39. if (!Array.isArray(json)) {
  40. throw new Error('输入的 JSON 不是数组');
  41. }
  42. return ParseJArray2List(json);
  43. }
  44. /**
  45. * 将json串转换成List对象,深度为1
  46. * @param jsonString
  47. * @returns
  48. */
  49. export function ParseJson2StringList(jsonString: string): string[] {
  50. const json = JSON.parse(jsonString);
  51. if (!Array.isArray(json)) {
  52. throw new Error('输入的 JSON 不是数组');
  53. }
  54. return ParseJArray2List(json, true).map(x => x.toString());
  55. }
  56. /**
  57. * 将Dictionary对象转换成json串
  58. * @param dict
  59. * @returns
  60. */
  61. export function ParseDictionary2Json<T>(dict: { [key: string]: T }): string {
  62. return JSON.stringify(dict);
  63. }
  64. /**
  65. * 将IList对象转换成json串
  66. * @param list
  67. * @returns
  68. */
  69. export function ParseList2Json(list: any[]): string {
  70. return JSON.stringify(list);
  71. }
  72. /**
  73. * 将JObject对象转换成Dictionary对象
  74. * @param json
  75. * @param depth1
  76. * @returns
  77. */
  78. function ParseJObject2Dictionary(json: any, depth1 = false): { [key: string]: any } {
  79. const result: { [key: string]: any } = {};
  80. for (const key in json) {
  81. if (json.hasOwnProperty(key)) {
  82. let value: any = null;
  83. const item = json[key];
  84. if (typeof item !== 'object' || item === null) {
  85. value = item;
  86. } else if (Array.isArray(item)) {
  87. value = depth1 ? item.toString() : ParseJArray2List(item);
  88. } else {
  89. value = depth1 ? JSON.stringify(item) : ParseJObject2Dictionary(item);
  90. }
  91. result[key] = value;
  92. }
  93. }
  94. return result;
  95. }
  96. /**
  97. * 将JArray对象转换成List对象
  98. * @param json
  99. * @param depth1
  100. * @returns
  101. */
  102. function ParseJArray2List(json: any[], depth1 = false): any[] {
  103. const result: any[] = [];
  104. for (const item of json) {
  105. let value: any = null;
  106. if (typeof item !== 'object' || item === null) {
  107. value = item;
  108. } else if (Array.isArray(item)) {
  109. value = depth1 ? item.toString() : ParseJArray2List(item);
  110. } else {
  111. value = depth1 ? JSON.stringify(item) : ParseJObject2Dictionary(item);
  112. }
  113. result.push(value);
  114. }
  115. return result;
  116. }