1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- using LitJson;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class JsonManage : MonoSingleton<JsonManage>
- {
- /// <summary>
- /// 通用解析Jsong 解析为任意类 ,请求成功返回 T ,失败返回null/0
- /// </summary>
- /// <typeparam name="T"> 需要解析的类型 </typeparam>
- /// <param name="message"></param>
- /// <returns></returns>
- public T ToJsonClass<T>(string message)
- {
- Debug.Log(message);
- JObject data = null;
- var isSucess = CheckMessage(message, out data);
- if (!isSucess)
- {
-
- return default(T);
- }
- message = data["data"].ToString();
- if (string.IsNullOrWhiteSpace(message) || message == "[]" || message == "{\r\n \"list\": []\r\n}")
- {
- return default(T);
- }
- T jsonClass = JsonMapper.ToObject<T>(message);
- return jsonClass;
- }
- /// <summary>
- /// 检查获取的信息是否正确
- /// </summary>
- /// <param name="message"></param>
- /// <param name="data"></param>
- /// <returns></returns>
- public bool CheckMessage(string message, out JObject data)
- {
- bool isSucess = true;
- data = JsonConvert.DeserializeObject<JObject>(message);
- if (data["code"].ToString() != "200")
- {
- isSucess = false;
- data = null;
- Debug.LogError("Json解析失败 " + message);
- }
- return isSucess;
- }
- }
|