Json----简单介绍
Json
先分享一个网站http://www.bejson.com/,这个是用来检测Json文件的错误的,Json文件一般不好查找错误.
看懂Json只需要四句话:
对象表示为键值对
数据由逗号分隔
花括号保存对象
方括号保存数组



这就是Json文件.不在过多介绍,重点不在这里
基础:Json到对象和对象到Json
需要用到的API:
JsonMapper.ToJson();
JsonMapper.ToObject();
引用命名空间Using LitJson;
代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;
public class JsonTest : MonoBehaviour {
void Start()
{
//对象到Json
MyIphone myPhone = new MyIphone
{
appNum = ,
phoneState = true,
appList = new List<string>() { "抖音", "BiliBili", "喜马拉雅听" }
};
string jsonIpone = JsonMapper.ToJson(myPhone);
Debug.Log(jsonIpone); //Json转到对象
string myIphoneJson = "{'appNum':25,'phoneState':false,'appList':['WeChat','Sina','QQ']}";
MyIphone myIPhone = JsonMapper.ToObject<MyIphone>(myIphoneJson);
Debug.Log("手机应用数量:"+myIPhone.appNum+"-手机开机状态"+myIPhone.phoneState);
for (int i = ; i < myIPhone.appList.Count; i++)
{
Debug.Log(myIPhone.appList[i]);
}
}
}
public class MyIphone
{
public int appNum;
public bool phoneState;
public List<string> appList;
}
进阶使用的:
代码二:
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using LitJson;
using UnityEngine;
public class DataNode// 该class用于json的时候不能有构造函数
{
public string CopyName;
public string CopyPosition;
public string CopyRotation;
}
public class DataCenter
{
public List<DataNode> List; public DataCenter()
{
List =new List<DataNode>();
}
}
public class JsonConvert : MonoBehaviour {
private string _txtPath;
private string _jsonPath;
void Start ()
{
_jsonPath = Application.streamingAssetsPath + "/CopyInfo.json";
_txtPath = Application.streamingAssetsPath + "/CopyInfo.txt";
// Json的解析是很快的 网络
ReadTextToJson();
ReadJsonFromJsonPath();
}
void ReadJsonFromJsonPath()
{
string jsondata = File.ReadAllText(_jsonPath);
List<DataNode> node = JsonMapper.ToObject<List<DataNode>>(jsondata);
Debug.LogError(node.Count);
}
void ReadTextToJson()
{
DataCenter dc = new DataCenter();
using (StreamReader reader = new StreamReader(_txtPath,Encoding.UTF8))
{
string tmpStr = string.Empty;
while ( !string.IsNullOrEmpty(tmpStr = reader.ReadLine()))
{
string[] infos = tmpStr.Split('_');
DataNode _node = new DataNode();
_node = infos();
dc.List.Add(_node);
}
}
//数据读取完毕 开始写入json 传递的List<>
string jsonData = JsonMapper.ToJson(dc.List);
File.WriteAllText(_jsonPath,jsonData);
}
}
代码三:
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
using Excel;
using OfficeOpenXml;
using Excel.Core;
using System.Data;
using OfficeOpenXml.Style;
using UnityEditor;
using LitJson;
class CopyInfo //CopyInfo的构造
{
public string CopyName;
public Vector3 CopyPosition;
public Quaternion CopyRotation;
public CopyInfo(string name, Vector3 postion, Vector3 rotation)
{
CopyName = name;
CopyPosition = postion;
CopyRotation = Quaternion.Euler(rotation);
}
}
class JsonNode//JsonNode的构造
{
public string StrName;
public string StrPosition;
public string StrRotation;
public JsonNode(string name,string pos, string rot)
{
StrName = name;
StrPosition = pos;
StrRotation = rot;
}
}
class MyJsonData //MyJsonData的构造
{
public List<JsonNode> JsonDatalist; public MyJsonData()
{
JsonDatalist = new List<JsonNode>();
}
}
public class CreateManager : MonoBehaviour
{
private List<GameObject> _copyList;
private List<CopyInfo> _copyInfoList;
private MyJsonData _jsonData;
private string _excelPath;
private string _path;
//1. 读文件
//2. 解析文件信息
//3. 实例化
//4. 实例化6个
//4.1 在3s中销毁一个并且添加一个
#region 读取存储在unity的streamingAssets路径中的json
void ReadJsonByJsonPath()
{
string path = Application.streamingAssetsPath + "/CopyInfo.json";//路径
string readInfo =File.ReadAllText(path);//文件读取过程
Debug.LogWarning(readInfo); //类型是JsonNode
List<JsonNode> JsonDatalist = new List<JsonNode>();//因为没有new对象!!!
JsonDatalist = JsonMapper.ToObject<List<JsonNode>>(readInfo);//重要代码
Debug.LogError(JsonDatalist.Count);
}
#endregion
void Start()
{
_path = Application.streamingAssetsPath + "/CopyInfo.txt";
_excelPath = Application.streamingAssetsPath + "/CopyInfo.xlsx";
_copyInfoList = new List<CopyInfo>();
_copyList = new List<GameObject>();
_jsonData = new MyJsonData();
WriteJsonByList();
ReadJsonByJsonPath();
}
#region
private void WriteJsonByList( )
{
string jsonPath = Application.streamingAssetsPath
+ "/CopyInfo.json";
////将一个list信息转换为json格式
////定义一个class 这个class是什么不重要 必须包含一个列表
_jsonData = new MyJsonData();
using (StreamReader reader = new StreamReader(_path,Encoding.UTF8))
{
string tmp = string.Empty;
while ( ! string.IsNullOrEmpty(tmp = reader.ReadLine()))
{
string[] infos = tmp.Split('_');
_jsonData.JsonDatalist.Add(new JsonNode(infos[], infos[], infos[]));
}
}
//jsondata 数据填充完成
string writeData = JsonMapper.ToJson(_jsonData);//
File.WriteAllText(jsonPath,writeData);
}
#endregion
private void WriteExcel(string path, List<CopyInfo> list)
{
FileInfo excelInfo = new FileInfo(path);
if (excelInfo.Exists)
{
excelInfo.Delete();
excelInfo = new FileInfo(path);
}
//开始shiyong Excel
using (ExcelPackage package = new ExcelPackage(excelInfo))
{
ExcelWorksheet sheet = package.Workbook.Worksheets.Add("TestInfo"); // 添加了一个工作表
sheet.Cells[, ].Value = "CopyName";
sheet.Cells[, ].Value = "CopyPosition";
sheet.Cells[, ].Value = "CopyRotation";
for (int i = ; i < _copyInfoList.Count; i++)
{
sheet.Cells[ + i, ].Value = _copyInfoList[i].CopyName;
sheet.Cells[ + i, ].Value = _copyInfoList[i].CopyPosition;
sheet.Cells[ + i, ].Value = _copyInfoList[i].CopyRotation;
}
sheet.Cells.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
sheet.Cells.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
sheet.Cells.Style.Font.Bold = true;
sheet.Cells.Style.Font.Name = "宋体";
sheet.Cells.Style.Font.Size = ;
sheet.Cells.AutoFitColumns(, );
package.Save();
}
AssetDatabase.Refresh();
}
}
Json----简单介绍的更多相关文章
- JSON简单介绍
//JSON是一种数据格式//JSON比较像php里面的关联数组,它里面存的内容也是key和value成对存在的 JSON写法格式 var js = { "one":"h ...
- JSON基础,简单介绍
JSON(JavaScript Object Notation(记号.标记)) 是一种轻量级的数据交换格式.它基于JavaScript(Standard ECMA-262 3rd Edition - ...
- iOS-iOS开发简单介绍
概览 终于到了真正接触IOS应用程序的时刻了,之前我们花了很多时间去讨论C语言.ObjC等知识,对于很多朋友而言开发IOS第一天就想直接看到成果,看到可以运行的IOS程序.但是这里我想强调一下,前面的 ...
- iOS开发拓展篇-XMPP简单介绍
iOS开发拓展篇-XMPP简单介绍 一.即时通讯简单介绍 1.简单说明 即时通讯技术(IM)支持用户在线实时交谈.如果要发送一条信息,用户需要打开一个小窗口,以便让用户及其朋友在其中输入信息并让交谈双 ...
- Linux curl使用简单介绍
在两台新搬迁的微信服务器上执行命令: curl -H "Content-Type: application/json" -d '{"partner_no":&q ...
- iOS开发——网络编程OC篇&(一)XMPP简单介绍与准备
XMPP简单介绍与准备 一.即时通讯简单介绍 1.简单说明 即时通讯技术(IM)支持用户在线实时交谈.如果要发送一条信息,用户需要打开一个小窗口,以便让用户及其朋友在其中输入信息并让交谈双方都看到交谈 ...
- Json.Net系列教程 1.Json.Net介绍及实例
原文 Json.Net系列教程 1.Json.Net介绍及实例 本系列教程假设读者已经对Json有一定的了解,关于Json在这里不多说.本系列教程希望能对读者开发涉及到Json的.Net项目有一定的帮 ...
- Django - Django框架 简单介绍
Django框架 简单介绍 本文地址: http://blog.csdn.net/caroline_wendy/article/details/29172271 1. 介绍 Django是一个开放源码 ...
- webpack入门篇--1.简单介绍
简单介绍: webpack是一个模块打包工具,给js准备的打包工具,可以把很多的模块打包成很少的文件 目标: 1.切分依赖数,分到不同代码块里,按需加载,懒加 载 2.任何静态资源都可以被视为一个模块 ...
- Protobuf的简单介绍、使用和分析
Protobuf的简单介绍.使用和分析 一.protobuf是什么? protobuf(Google Protocol Buffers)是Google提供一个具有高效的协议数据交换格式工具库( ...
随机推荐
- 【Ubuntu 18.04 搭建VNC服务器】
https://www.jianshu.com/p/f58fe5cdeb5f 桌面共享 Ubuntu 18.04自带桌面共享,可以将物理桌面共享给VNC.但是无法创建新的桌面. 具体参考 https: ...
- django orm 及常用参数
一些说明: 表myapp_person的名称是自动生成的,如果你要自定义表名,需要在model的Meta类中指定 db_table 参数,强烈建议使用小写表名,特别是使用MySQL作为后端数据库时. ...
- Linux Centos7.5中的RocketMQ集群部署
系统环境 Docker > centos7.5 此镜像已经安装了jdk1.8和maven3.6.0 如果你想知道这个基础镜像的具体情况, 参考此文: https://www.cnblogs.co ...
- jq 操作表单中 checkbox 全选 单选
知识点: Note: 1: .prop() 和 .attr() 方法的区别 .prop() 针对标签既有属性 .attr() 针对自定义属性 2: $('input:checked')即为选中元素. ...
- ASP.NET Core微服务 on K8S学习笔记(第一章:详解基本对象及服务发现)
课程链接:http://video.jessetalk.cn/course/explore 良心课程,大家一起来学习哈! 任务1:课程介绍 任务2:Labels and Selectors 所有资源对 ...
- MyCat读写分离-笔记(四)
概述 Mycat能够实现数据库读写分离,不能实现主从同步,数据库的备份还是基于数据库层面的.Mycat只是数据库的中间件: Mycat读写分离配置 在MySQL中间件出现之前,对于MySQL主从集群, ...
- React使用Mobx管理数据
React 和 Vue一样都属于单向数据流,为了更好的进行状态和数据管理官方和第三方也有配套的Redux等插件,本文介绍一个个人觉得更易用使用的组件 Mobx 核心概念 MobX 处理你的应用程序状态 ...
- Ansible入门篇:playbook的使用
playbooks介绍 playbooks是 一个不同于使用Ansible命令行执行方式的模式,其功能更强大灵活.简单来说,playbook是一个非常简单的配置管理和多主机部署系统,不同于任何已经存在 ...
- Django“少折腾”
1.Django中文语言.时区 修改项目setting文件 LANGUAGE_CODE = 'zh-hans' TIME_ZONE = 'Asia/Shanghai'
- C语言第零次作业
Q1.你对网络专业或者计算机专业了解是怎样? 说实话不了解网络专业,在甚至在填志愿之前我都不曾听说过.但经过一番的查阅资料.现在,首先我了解到我们主要学习计算机.通信以及网络方面的基础理论.设计原理, ...