Skip to content

Commit

Permalink
Merge pull request #39 from hongshaodalao/patch-3
Browse files Browse the repository at this point in the history
Update TableUtil.cs
  • Loading branch information
MikeJinhua authored Jul 31, 2019
2 parents 38ab02d + 76c9a9f commit 9ee7143
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion Client/Assets/Scripts/Util/TableUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class TableUtil

public static int[] LoadInts(string data)
{
int[] decodeData = JsonUtility.FromJson<int[]>(data);
int[] decodeData = FromJson<int[]>(data);
return decodeData;
}

Expand All @@ -17,4 +17,27 @@ public static Dictionary<string,int> LoadDictString2Int(string data)
Dictionary<string, int> decodeData = JsonUtility.FromJson<Dictionary<string, int>>(data);
return decodeData;
}

/// <summary> 解析Json </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="json">Json字符串</param>
public static T FromJson<T>(string json)
{
if (json == "null" && typeof(T).IsClass) return default(T);

if (typeof(T).GetInterface("IList") != null)
{
json = "{\"data\":{data}}".Replace("{data}", json);
Pack<T> Pack = JsonUtility.FromJson<Pack<T>>(json);
return Pack.data;
}

return JsonUtility.FromJson<T>(json);
}

/// <summary> 内部包装类 </summary>
private class Pack<T>
{
public T data;
}
}

0 comments on commit 9ee7143

Please sign in to comment.