Unity3D_(数据)JsonUtility创建和解析Json

 

 

  Json  百度百科:传送门

  LitJson创建和解析Json  传送门

  Json数据解析在Unity3d中的应用  传送门

 

一、使用JsonUnity创建Json

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

[Serializable]
public class Person
{
    public string name;
    public int age;
}

public class JSON_Gary : MonoBehaviour {

    // Use this for initialization
    void Start () {
        //Json操作 两种方式 ListJson JsonUtility
        //使用代码的方式创建一个json
        //{'name':'Gary','age':20}

        Person p1 = new Person();
        p1.name = "Gary";
        p1.age = 20;
        //转成json字符串
        string jsonStr = JsonUtility.ToJson(p1);
        Debug.Log(jsonStr);

    }
    
}
JSON_Gary.cs

 

 

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

[Serializable]
public class Person
{
    public string name;
    public int age;
}


[Serializable]
public class Persons
{
    public Person[] persons;
}

public class JSON_Gary : MonoBehaviour {

    // Use this for initialization
    void Start () {
        //Json操作 两种方式 ListJson JsonUtility
        //使用代码的方式创建一个json
        //{'name':'Gary','age':20}

        Person p1 = new Person();
        p1.name = "Gary";
        p1.age = 20;
        //转成json字符串
        string jsonStr = JsonUtility.ToJson(p1);
        //Debug.Log(jsonStr);

        //{'persons':[{'name':'Gary','age':20},{'name':'Gary2','age':25}]}
        Person p2 = new Person();
        p2.name = "Gary2";
        p2.age = 25;
        Person[] ps = new Person[] { p1, p2 };

        Persons persons = new Persons();
        persons.persons = ps;
        jsonStr = JsonUtility.ToJson(persons);
        Debug.Log(jsonStr);

    }
    
}
JSON_Gary.cs

 

 

二、使用JsonUtility解析Json

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

[Serializable]
public class Person
{
    public string name;
    public int age;
}


[Serializable]
public class Persons
{
    public Person[] persons;
}

public class JSON_Gary : MonoBehaviour {

    // Use this for initialization
    void Start () {
        //Json操作 两种方式 ListJson JsonUtility
        //使用代码的方式创建一个json
        //{'name':'Gary','age':20}

        Person p1 = new Person();
        p1.name = "Gary";
        p1.age = 20;
        //转成json字符串
        string jsonStr = JsonUtility.ToJson(p1);
        //Debug.Log(jsonStr);

        //{'persons':[{'name':'Gary','age':20},{'name':'Gary2','age':25}]}
        Person p2 = new Person();
        p2.name = "Gary2";
        p2.age = 25;
        Person[] ps = new Person[] { p1, p2 };

        Persons persons = new Persons();
        persons.persons = ps;
        jsonStr = JsonUtility.ToJson(persons);
        //jsonStr ={ 'persons':[{'name':'Gary','age':20},{'name':'Gary2','age':25}]}
        //Debug.Log(jsonStr);

        //解析Json
        Persons newPersons = JsonUtility.FromJson<Persons>(jsonStr);
        Debug.Log(newPersons.persons[0].name);

    }
    
}
JSON_Gary.cs

 

https://www.cnblogs.com/qiaogaojian/p/6532665.html

转载于:https://www.cnblogs.com/1138720556Gary/p/9944079.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值