zoukankan      html  css  js  c++  java
  • 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

    (如需转载学习,请标明出处)
  • 相关阅读:
    javascript异步编程学习及实例
    通过调试vue-cli 构建代码学习vue项目构建运行过程
    web技术栈开发原生应用-多端共用一套代码
    vuejs应用开发前后端分离
    web前端跨域解决方案JSONP,CORS,NGINX反向代理
    js script 加载顺序
    js 百度地图
    layui-table 样式
    css 排版 test
    layui table
  • 原文地址:https://www.cnblogs.com/1138720556Gary/p/9944079.html
Copyright © 2011-2022 走看看