KITASENJU DESIGN BLOG

memo, html, javascript, unity

How to use JSONObject

JSONObject使い方

import

using UnityEngine;
using System.IO;

public class JsonLoader : MonoBehaviour
{
    public TextAsset data;

   // private List<Vector3> _hoge;

    void Start()
    {

        Debug.Log(data.text);
        
        //読み込む
        var a = new JSONObject(data.text);
        var n = a.GetField("Hoge");

        //取得する 数値は.f 文字列は.sか.strか忘れた、など
        //Debug.Log( n[0][0].f + 1.99f );

        //追加する
        a.AddField("abc",10f);

        //保存する
        string filePath = Application.dataPath +"/test01.json";
        File.WriteAllText (filePath, a.ToString());

        Debug.Log( a.ToString() );
        

    }
}

export

var obj = new JSONObject(JSONObject.Type.OBJECT);

        var points = new JSONObject(JSONObject.Type.ARRAY);
        obj.AddField("points",points);

        for(int i=0;i<_points.Count;i++){
            var p = _points[i];
            var o = new JSONObject(JSONObject.Type.OBJECT);

            o.AddField("x", p.point.x);
            o.AddField("y", p.point.y);
            o.AddField("z", p.point.z);
            o.AddField("anim",p.anim.ToString());
            points.Add(o);

        }

        string filePath = Application.dataPath + "/" + path + jsonName + ".json";
        Debug.Log("path " + filePath);
        System.IO.File.WriteAllText (filePath, obj.ToString(true));
"FOOTER"