NewN_UAVPlane/Assets/Temp/Scripts/Jsonanalyze.cs

44 lines
1.2 KiB
C#

using LitJson;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Jsonanalyze : MonoBehaviour
{
/// <summary>
/// 通过网络字符串转换成对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="str"></param>
/// <returns></returns>
public static T FromJson<T>(string str)
{
if (str != null && !string.IsNullOrEmpty(str))
{
str = str.Replace("\\\"", "\"");
// str = str.Replace("\\n", "\n");
// str = str.Replace(" ", "");
// str = str.Replace("\t", "");
if (str.Length > 2)
{
if (str[0] == '"')
{
str = str.Substring(1, str.Length - 1);
str = str.Substring(0, str.Length - 1);
}
// Debug.Log("FromJson字符串:"+ str);
return JsonMapper.ToObject<T>(str);
}
else
{
Debug.LogError("字符串长度不够!");
}
}
else
{
Debug.LogError("字符串错误!");
}
return default(T);
}
}