unity Json

原创 2015年07月08日 14:28:18

 

//本文主要介绍在unity中如何处理json数据
//LitJson是处理JSON的第三方库,需要下载导入
//可直接右键Assets,Import New Asset导入LitJson
using UnityEngine;
using System.Collections;
using LitJson;
using System.IO;
using System.Text;

public class JsonDemo : MonoBehaviour
{
    public TextAsset json;
    public string filePath;
    public string fileName;
    void Awake()
    {
        filePath = Application.dataPath + "/Resources/TextFile";
        fileName = filePath + "/File.json";
        //StringToJson();
        //ObjectToJson();
        //WriteJsonAndLog();
        //writeJsonToFile(filePath,fileName);
        //ReadJsonFromJson();
    }

    public void StringToJson()
    {
        //@ 换行
        string str = @"{'name':'hello','age':16,'items':
[{'height':100,'width':'20'},{'height':'30','width':'40'}]}";

        JsonData jd = JsonMapper.ToObject(str);
        Debug.Log("name:" + (string)jd["name"]);
        Debug.Log("age:" + (int)jd["age"]);

        JsonData jdItems = jd["items"];
        for (int i = 0; i < jdItems.Count; i++)
        {
            Debug.Log("height" + jdItems[i]["height"]);
            Debug.Log("width" + jdItems[i]["width"]);
        }

    }

    public void ObjectToJson()
    {
        Person person = new Person();
        person.age = 10;
        person.name = "caicai";

        string strjson = JsonMapper.ToJson(person);
        Debug.Log(strjson);
        JsonData jd = JsonMapper.ToObject(strjson);
        Debug.Log("name" + jd["name"]);
        Debug.Log("age" + jd["age"]);
    }


    public void WriteJsonAndLog()
    {
        StringBuilder strB = new StringBuilder();
        JsonWriter jsWrite = new JsonWriter(strB);
        jsWrite.WriteObjectStart();
        jsWrite.WritePropertyName("name");
        jsWrite.Write("caicai");
        jsWrite.WritePropertyName("list");
        jsWrite.WriteArrayStart();
        jsWrite.WriteObjectStart();
        jsWrite.WritePropertyName("name");
        jsWrite.Write("huahua");
        jsWrite.WriteObjectEnd();
        jsWrite.WriteObjectStart();
        jsWrite.WritePropertyName("name");
        jsWrite.Write("huanghuang");
        jsWrite.WriteObjectEnd();
        jsWrite.WriteArrayEnd();
        jsWrite.WriteObjectEnd();
        Debug.Log(strB);
        JsonData jd = JsonMapper.ToObject(strB.ToString());
        Debug.Log("name"+jd["name"]);
        JsonData items = jd["list"];
        for (int i = 0; i < items.Count; i++)
        {
            Debug.Log(items[i]["name"]);
        }
    }

    public void writeJsonToFile(string path, string fileName)
    {
        StringBuilder strB = new StringBuilder();
        JsonWriter jsWrite = new JsonWriter(strB);
        jsWrite.WriteObjectStart();
        jsWrite.WritePropertyName("name");
        jsWrite.Write("caicai");
        jsWrite.WritePropertyName("list");
        jsWrite.WriteArrayStart();
        jsWrite.WriteObjectStart();
        jsWrite.WritePropertyName("name");
        jsWrite.Write("huahua");
        jsWrite.WriteObjectEnd();
        jsWrite.WriteObjectStart();
        jsWrite.WritePropertyName("name");
        jsWrite.Write("huanghuang");
        jsWrite.WriteObjectEnd();
        jsWrite.WriteArrayEnd();
        jsWrite.WriteObjectEnd();

        DirectoryInfo dir = new DirectoryInfo(path);
        if (dir.Exists)
        {
            Debug.Log("dir is exit");
        }
        else
        {
            Directory.CreateDirectory(path);
            Debug.Log("createdir");
        }
        StreamWriter sw;
        if (File.Exists(fileName))
        {
            //sw = File.AppendText(fileName);
            sw = File.CreateText(fileName);
            Debug.Log("appendText");
        }
        else
        {
            sw = File.CreateText(fileName);
            Debug.Log("createText");
        }
         sw.WriteLine(strB);
        sw.Close();
        //AssetDatabase.Refresh();
    }

    public void ReadJsonFromJson()
    {
        json = Resources.Load("TextFile/File") as TextAsset;
        JsonData jd = JsonMapper.ToObject(json.text);
        Debug.Log(jd["name"]);
    }
}


class Person
{
    public string name { get; set; }
    public int age { get; set; }
}

 

NGUIJson Unity 的Json工具

  • 2017年10月24日 16:39
  • 3KB
  • 下载

JSON NET For Unity 9.0.1

  • 2017年07月06日 18:41
  • 214KB
  • 下载

unity3d学习笔记(二十)--利用JSON读取和保存游戏数据

本系列文章由Aimar_Johnny编写,欢迎转载,转载请标明出处,谢谢。 http://blog.csdn.net/lzhq1982/article/details/19237031 ...

JSON NET For Unity 2.0.1

  • 2016年08月02日 16:37
  • 1.37MB
  • 下载

json.net for unity

  • 2017年05月05日 17:40
  • 1.37MB
  • 下载

【Unity&JSON】JsonUtility的多对象读写(3)

原文内容来自:Read from and write to external .json files using JsonUtilities or LitJson 文件Unity 版本号5.3,使用时...

JSON .NET For Unity v1.3.5

  • 2015年06月25日 18:49
  • 665KB
  • 下载

Unity 读写Json数据:LitJSON快速教程

出处:http://www.cnblogs.com/neverdie/ 欢迎转载,也请保留这段声明 介绍 JSON是一个简单的,但功能强大的序列化数据格式。它定义了简单的类型,如布尔,...

JSON NET For Unity 1.4.0

  • 2016年08月02日 15:59
  • 493KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:unity Json
举报原因:
原因补充:

(最多只允许输入30个字)