栏目分类:
子分类:
返回
终身学习网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
终身学习网 > IT > 软件开发 > 游戏开发 > Unity3D

Unity---Json

Unity3D 更新时间:发布时间: 百科书网 趣学号

【千锋合集】史上最全Unity3D全套教程|匠心之作_哔哩哔哩_bilibili

网站 Json.cn 用于Json的检测

 

 

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Json;
public class JsonGenerate : MonoBehaviour {
    string json = "{'name':'xiaoming','age':18}";
    private void Awake()
    {        
    }
    private void Start()
    {
        JsonObject obj = new JsonObject();
        //存储位置
        obj.Add("position",transform.position.ToString());
        //存储颜色
        obj.Add("color", GetComponent().material.color.ToString());
        //打印
        Debug.Log(obj.ToString());

//批量添加
        KeyValuePair[] kvs;
        kvs = new KeyValuePair[3];
        kvs[0] = new KeyValuePair("IsKinematic", true);
        kvs[1] = new KeyValuePair("UseGravity", false);
        kvs[2] = new KeyValuePair("IsTrigger", false);
        obj.AddRange(kvs);
    }

    private void JsonBase()
    {
        //创建Json对象,相当于一个{}
        JsonObject person_Obj = new JsonObject();
        //向大括号中添加键值对
        person_Obj.Add("name","xiaoming");
        person_Obj.Add("age",18);
        //创建json数组,相当于一个中括号
        JsonArray scores = new JsonArray();
        //向数组中添加元素
        scores.AddRange(90,"缺考","优秀");
        //语文成绩
        JsonObject chineseScore = new JsonObject();
        chineseScore.Add("语文", 75);
        scores.Add(chineseScore);
        //添加数组元素
        person_Obj.Add("scores",scores);
        //打印Json
        Debug.Log(person_Obj.ToString());
        
    }

}
 

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

public class JsonParse : MonoBehaviour {
    string json = "{'name':'xiaoming','age':18}";
    private void Start()
    {
        //解析Json
        JsonData data=JsonMapper.ToObject(json);
        //打印结果
        Debug.Log(data["name"]);
        //Debug.Log(data["技能"][0]["技能名称"]);
        //for (int i = 0; i < data["技能"].Count; i++) {
        //    Debug.Log(data["技能"][i]["技能名称"]);
        //}


    }
}
 

 

 

 

 

 

 

 

 

转载请注明:文章转载自 www.051e.com
本文地址:http://www.051e.com/it/1070853.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 ©2023-2025 051e.com

ICP备案号:京ICP备12030808号