Last active
November 23, 2019 09:41
-
-
Save masayuki5160/7972289 to your computer and use it in GitHub Desktop.
UnityでJsonをあつかう
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// JSONのテキスト作成 | |
$dummy[0]["name"] = "google"; | |
$dummy[0]["url"] = "https://www.google.co.jp/"; | |
$json = json_encode($dummy); | |
// JSON用のヘッダを定義して出力 | |
header("Content-Type: text/javascript; charset=utf-8"); | |
echo $json; | |
exit(); | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
using MiniJSON; | |
public class TestHttp : MonoBehaviour { | |
private string res; | |
// Use this for initialization | |
void Start () { | |
StartCoroutine (Download()); | |
} | |
// Update is called once per frame | |
void Update () { | |
} | |
IEnumerator Download() | |
{ | |
// リクエスト先は仮 | |
WWW www = new WWW("http://hoge.com/testapi.php"); | |
// wait until HTML Contents will come | |
yield return www; | |
if(www.error != null){ | |
Debug.Log("Error!"); | |
}else{ | |
Debug.Log("Success"); | |
// JSONデータは最初は配列から始まるので、Deserialize(デコード)した直後にリストへキャスト | |
IList jsonList = (IList)Json.Deserialize(www.text); | |
// リストの内容はオブジェクトなので、辞書型の変数に一つ一つ代入しながら、処理 | |
foreach(IDictionary param in jsonList){ | |
// nameは文字列なので、文字列型へキャストしてから変数へ格納 | |
string name = (string)param["name"]; | |
Debug.Log("name:"+name); | |
} | |
} | |
} | |
} |
もう一個参考サイト➡
UnityでJSONをあつかう - MiniJSON
http://neareal.net/index.php?ComputerGraphics%2FUnity%2FTips%2FJSON%2FMiniJSON
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
参考サイト➡
[Unity][MiniJSON]JSONデータを読み込む
http://www.cho-design-lab.com/2013/08/15/unity-minijson-load-json/