因應專案需求,需要與後台串接api,目前一般都是使用json格式來傳,所以在unity中有兩種方式一種使用網路上流傳的SimpleJSON針對keyword來取值,一種使用JsonUtility

以下為使用二種解析的方法

SimpleJSON

相關參考文件:http://wiki.unity3d.com/index.php/SimpleJSON

使用方式:

於Unity中加上SimpleJSON.cs

var N = JSON.Parse(the_JSON_string);
var versionString = N["keyword"]

需要注意的事若取array,只能使用for, 無法使用foreach取值

 

JsonUtility

相關參考文件:https://stackoverflow.com/questions/36239705/serialize-and-deserialize-json-and-json-array-in-unity,https://docs.unity3d.com/Manual/JSONSerialization.html

使用方式:

1.使用http://json2csharp.com/ 產出json相關class

2.於要使用的Code上加上using System;

3.於產出的class上加上[Serializable]

4.移除所有變數的{ get; set; }

5.List<T>改成T[] 不使用List改用array取代才行

arrow
arrow

    狼翔月影 發表在 痞客邦 留言(0) 人氣()