123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- using SimpleJSON;
- using UnityEngine;
- using System;
- namespace WJ{
- public class JsonUtility{
- public static Vector3 GetVec(JSONArray json ){
- return new Vector3(json[0].AsFloat, json[1].AsFloat, json[2].AsFloat);
- }
- public static JSONArray GetVecJson(Vector3 vec ){
- JSONArray result = new JSONArray();
- result.Add(new JSONData(vec.x) );
- result.Add(new JSONData(vec.y) );
- result.Add(new JSONData(vec.z) );
- return result;
- }
- public static Vector2Int GetVecFromJson(JSONNode vecJson ){
- JSONArray vecArr = vecJson.AsArray;
- return new Vector2Int(
- vecArr[0].AsInt,
- vecArr[1].AsInt
- );
- }
- public static Vector3 GetVecFromStr(string vecStr ){
- string[] tempStrs = vecStr.Split(',');
- return new Vector3(
- float.Parse(tempStrs[0]),
- float.Parse(tempStrs[1]),
- float.Parse(tempStrs[2])
- );
- }
- public static JSONArray GetArray(string arrayStr ){
- JSONArray result = JSONArray.Parse(arrayStr).AsArray;
- return result;
- }
- public static float GetFloatValue(JSONNode json, float defaultValue ){
- if (string.IsNullOrEmpty(json.ToString()) ){
- return defaultValue;
- }
- return json.AsFloat;
- }
- public static long GetLongValue(JSONNode json, long defaultValue ){
- if (string.IsNullOrEmpty(json.ToString()) ){
- return defaultValue;
- }
- return json.AsLong;
- }
- public static void SetNodeIfNoEmpty(
- JSONNode json, string key, string value, string defaultValue = "0"){
- if (value != defaultValue ){
- json[key] = value;
- }
- }
- public static string UnEscapeJavascriptString(string jsonString){
-
- if (String.IsNullOrEmpty(jsonString))
- return jsonString;
-
- System.Text.StringBuilder sb = new System.Text.StringBuilder();
- char c;
-
- for (int i = 0; i < jsonString.Length; )
- {
- c = jsonString[i++];
-
- if (c == '\\')
- {
- int remainingLength = jsonString.Length - i;
- if (remainingLength >= 2)
- {
- char lookahead = jsonString[i];
- if (lookahead == '\\')
- {
- sb.Append('\\');
- ++i;
- }
- else if (lookahead == '"')
- {
- sb.Append("\"");
- ++i;
- }
- else if (lookahead == 't')
- {
- sb.Append('\t');
- ++i;
- }
- else if (lookahead == 'b')
- {
- sb.Append('\b');
- ++i;
- }
- else if (lookahead == 'n')
- {
- sb.Append('\n');
- ++i;
- }
- else if (lookahead == 'r')
- {
- sb.Append('\r');
- ++i;
- }else if (lookahead == 'u'){
- char[] hex = new char[4];
-
- for (int m=0; m< 4; m++) {
- hex[m] = jsonString[i+m+1];
- }
-
- sb.Append((char) Convert.ToInt32(new string(hex), 16));
- i++;
- i += 4;
- }
- }
- }
- else
- {
- sb.Append(c);
- }
- }
- //Debug.Log(sb.ToString() );
- return sb.ToString();
- }
- }}
|