kidoOooOoooOOom

ゲーム開発やってます

UnityScriptでJSONデータをHTTP GETしてパース

まずはminiJSONを下記から取得して、Assets/Pluginsに格納
http://forum.unity3d.com/threads/35484-MiniJSON-script-for-parsing-JSON-data

練習がてら、下記のようなJSONを取得してみます。

{
  "x": 10,
  "y": 200,
  "z": 30
}

localhostにたててるnode.jsからJSONファイルを取得してログ出力。

#pragma strict

import System.Collections.Generic;
import MiniJSON;

function Start(){
    var url = "http://localhost:3000/resources/sample.json";
    var www:WWW = new WWW(url);
    yield www;
    
    var json = www.text;
    Debug.Log(json);
    
    var dict = Json.Deserialize(json) as Dictionary.<String, System.Object>;
    Debug.Log(dict);
    var x = dict["x"];
    var y = dict["y"];
    
    Debug.Log("x is " + x + ", y is " + y);    
}

これで取れるはz