Skip to content

Instantly share code, notes, and snippets.

@masayuki5160
Last active November 23, 2019 09:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masayuki5160/7972289 to your computer and use it in GitHub Desktop.
Save masayuki5160/7972289 to your computer and use it in GitHub Desktop.
UnityでJsonをあつかう
<?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();
?>
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);
}
}
}
}
@masayuki5160
Copy link
Author

参考サイト➡
[Unity][MiniJSON]JSONデータを読み込む
http://www.cho-design-lab.com/2013/08/15/unity-minijson-load-json/

@masayuki5160
Copy link
Author

もう一個参考サイト➡
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