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