unity Json

 

//本文主要介绍在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; }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值