Unity之JSON資料解析

來源:互聯網
上載者:User

unity在 5.3版本之後,添加了對json資料解析的支援,對於json資料的解析可以使用內建類JsonUtility來進行解析。
例:解析字串

string userLoginInfo = "{\"data\":{\"role\":\"ROLE_FAB_FAT\",\"type\":1,\"nickname\":\"jj\",\"avatar\":\"http://oan6vegks.bkt.clouddn.com/583e931ce12311ffb76d3c\",\"token\":\"Bearer eyJhbGciOiJIUzUxMiJ9.eyJyb2xlc132133TEVfRkFCUklDX0ZBQ1RPUlkiLCJzdWIiOiIxMiIsImlzcyI6IjEyIiwiaWF0IjowfQ.69sc3v-bfTws9MeSRD9aHvk_g2Sf2jpoI9KqU3oQ6T9WxV8nwwyf3LxkcC3WVJB4g8CV_hayjwep_vw8bQudTw\"}"

先建立一個類,命名TestModel,類裡面有屬性如下:

public class TestModel  {    public string role;    public int type;    public string nickname;    public string avatar;    public string token;}

只要在方法中調用

TestModel model = JsonUtility.FromJson<TestModel>(userLoginInfo);

這樣就能順利解析json字串,但是在如果TestModel在某個類裡面,json字串會無法解析,列印資料的時候會報錯。
例如在開發過程中,返回的json資料有一個固定模式,返回code狀態代碼,message資訊以及我們需要的資料,所以會用到泛型資料。例:

string userLoginInfo = "{\"code\":200,\"message\":\"Success.\",\"data\":{\"role\":\"ROLE_FAB_FAT\",\"type\":1,\"nickname\":\"jj\",\"avatar\":\"http://oan6vegks.bkt.clouddn.com/583e931ce12311ffb76d3c\",\"token\":\"Bearer eyJhbGciOiJIUzUxMiJ9.eyJyb2xlc132133TEVfRkFCUklDX0ZBQ1RPUlkiLCJzdWIiOiIxMiIsImlzcyI6IjEyIiwiaWF0IjowfQ.69sc3v-bfTws9MeSRD9aHvk_g2Sf2jpoI9KqU3oQ6T9WxV8nwwyf3LxkcC3WVJB4g8CV_hayjwep_vw8bQudTw\"}}";

這樣的json字串,那麼
我們需要建立一個泛型類,來接收返回的資料,類結構具體如下:

[SerializeField]public class ResponseMessageModel<T> {    public int code;    public string message;    public T data;}

data就是返回的資料,在應用中,我們進行解析的代碼修改為:

ResponseMessageModel<TestModel> model = JsonUtility.FromJson<ResponseMessageModel<TestModel>>(userLoginInfo);        Debug.Log("" + model.code);        Debug.Log("" + model.message);        Debug.Log("" + model.data.type);        Debug.Log("" + model.data.avatar);        Debug.Log("" + model.data.nickname);        Debug.Log("" + model.data.token);

但是如果使用unity提供的JsonUtility資料類進行解析,則會發生錯誤在執行Debug.Log(“” + model.data.type);這行代碼的時候報錯,報錯資訊為: NullReferenceException: Object reference not set to an instance of an object

那麼行不通,就只能換一種方法,那就是使用LitJson,ListJson是一個第三方庫,提供給C#進行解析json資料。只需要下載ListJson.dll檔案,並且匯入到需要用的Unity項目中Plugin檔案夾下,在C#代碼中進行引用,就可以使用了。
將上面的代碼修改為:

 JsonReader fr = new JsonReader(userLoginInfo); ResponseMessageModel<TestModel> model = JsonMapper.ToObject<ResponseMessageModel<TestModel>>(fr);                Debug.Log("" + model.code);                Debug.Log("" + model.message);                Debug.Log("" + model.data.type);                Debug.Log("" + model.data.avatar);                Debug.Log("" + model.data.nickname);                Debug.Log("" + model.data.token);

可以通過下面的連結直接進行下載ListJson檔案
http://ok4xyxz61.bkt.clouddn.com/LitJson.dll

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.