Unity5.3Json测试学习

一.unity5.3新增了生成json、解析json和重写json的api,这里简单下。

namespace UnityEngine
{
   //解析json用的工具类
    public static class JsonUtility
    {
        //
        // 摘要:
        //     ///
        //     通过一个json和一个指定的父节点类型,返回这个父节点类的实例
        //     ///
        //
        // 参数:
        //   json:
        //     json格式的string字符串
        //
        //   type:
        //     根结点类型
        //
        // 返回结果:
        //     ///
        //     <pre name="code" class="csharp"><span style="white-space:pre">		</span>根节点类的实例
// /// [WrapperlessIcall] public static object FromJson(string json, Type type);


<span style="white-space:pre">	</span>//上面的一个泛型方法
        public static T FromJson<T>(string json);
        //
        // 摘要:
        //     ///
        //     制定一个根结点类,重写以这个根结点为基础的json
        //     ///
        //
        // 参数:
        //   json:
        //     重写的json,即新的json字符串
        //
        //   objectToOverwrite:
        //     需要重写的根结点类实例
        [WrapperlessIcall]
        public static void FromJsonOverwrite(string json, object objectToOverwrite);
        //
        // 摘要:
        //     ///
        //     将一个根结点为obj的“类树”解析为json字符串
        //     ///
        //
        // 参数:
        //   obj:
        //     根类
        //
        //   prettyPrint:
        //     If true, format the output for readability. If false, format the output for minimum
        //     size. Default is false.
        //
        // 返回结果:
        //     ///
        //     json字符串
        //     ///
        public static string ToJson(object obj);
        //
        // 摘要:
        //     ///
        //     和上面一样,格式化是否格式化输出
        //     ///
        //
        // 参数:
        //   obj:
        //     The object to convert to JSON form.
        //
        //   prettyPrint:
        //     如果是true,格式化输出,如果不是,以最省略形式输出,就是json文件就是一行,没有换行        //
        // 返回结果:
        //     ///
        //     The object's data in JSON format.
        //     ///
        [WrapperlessIcall]
        public static string ToJson(object obj, bool prettyPrint);
    }
}

二.示例程序

JsonClass.cs 用于构建类结构

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

[Serializable]
public class University
{
    public string UniversityName;
    //专业
    public Specialty specialty;
    //专业代号
}

[Serializable]
public class Specialty
{
    public string SpecialtyName;
    //人数
    public int StudentCount;
    //学生

    public Student student1;
    public Student student2;
    public Student student3;
}

[Serializable]
public class Student
{
    //学生姓名
    public string StudentName;
    //成绩
    public int StudengScore;
}

MyTestJson.cs 用于挂在GameObject中测试:

using UnityEngine;
using System.Collections;

public class MyTestJson : MonoBehaviour {

	// Use this for initialization
	void Start () {
        string json = writeJson();
        Debug.Log(json);
        int firstStuNum = getFirstSpecialtyStuNums();
        Debug.Log("第一个班级软加班有学生人数: " + firstStuNum + "个");
        
        
    }
	
	// Update is called once per frame
	void Update () {
	
	}

    //编写一个大学信息的json格式
    public string writeJson()
    {
        University uni = new University();
        uni.UniversityName = "qiongzhou";
        uni.specialty = new Specialty()
        {
            SpecialtyName = "SoftWare",
            StudentCount = 2,
            
            
             
            
        };
        uni.specialty.student1 = new Student()
        {
            StudentName = "蛋蛋君",
            StudengScore = 0
        };
        uni.specialty.student2 = new Student()
        {
            StudentName = "死羊君",
            StudengScore = 59
        };
        string json =  JsonUtility.ToJson(uni);
        return json;

    }

    public int getFirstSpecialtyStuNums()
    {
        University uni = new University();
        uni = JsonUtility.FromJson<University>(writeJson());

        int num = uni.specialty.StudentCount;
        return num;
    }

测试结果:


--------------------------------------------------------------------------------------------------------------------------------------------------------------

再测试下重写json的

public static void FromJsonOverwrite(string json, object objectToOverwrite);方法
源代码:
<pre name="code" class="csharp">using UnityEngine;
using System.Collections;

public class MyTestJson : MonoBehaviour {

	// Use this for initialization
	void Start () {
        string json = writeJson();
        
        
    }
	
	// Update is called once per frame
	void Update () {
	
	}

    //编写一个大学信息的json格式
    public string writeJson()
    {
        University uni = new University();
        uni.UniversityName = "qiongzhou";
        uni.specialty = new Specialty()
        {
            SpecialtyName = "SoftWare",
            StudentCount = 2,
            
            
             
            
        };
        uni.specialty.student1 = new Student()
        {
            StudentName = "蛋蛋君",
            StudengScore = 0
        };
        uni.specialty.student2 = new Student()
        {
            StudentName = "死羊君",
            StudengScore = 59
        };
        string json =  JsonUtility.ToJson(uni);
        Debug.Log(json);
        string newjson = writeJson2();
        JsonUtility.FromJsonOverwrite(newjson, uni);
        Debug.Log("修改后的json :" + JsonUtility.ToJson(uni));
        return json;

    }

   

    public string writeJson2()
    {
        University newuni = new University();
        newuni.UniversityName = "HaiDa";
        newuni.specialty = new Specialty()
        {
            SpecialtyName = "SoftWare",
            StudentCount = 3,




        };
        newuni.specialty.student1 = new Student()
        {
            StudentName = "小鸟游",
            StudengScore = 10
        };
        newuni.specialty.student2 = new Student()
        {
            StudentName = "乱入君",
            StudengScore = 20
        };
        newuni.specialty.student3 = new Student()
        {
            StudentName = "凑数君",
            StudengScore = 30
        };
        return JsonUtility.ToJson(newuni);
    }
}

测试结果:


<img src="https://img-blog.csdn.net/20151219021843376?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="" />
从表面上看就是这样的,挺好用的,不用自己写格式化json了。恩恩

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章