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提供一个具有高效的协议数据交换格式工具库( ...
随机推荐
- ActiveMQ简单使用
// 第一步:创建ConnectionFactory对象,需要指定服务端ip及端口号. //brokerURL服务器的ip及端口号 ConnectionFactory connectionFactor ...
- 详解MariaDB数据库的存储过程
1.什么是存储过程 很多时候,SQL语句都是针对一个或多个表的单条语句.但是也有时候有的查询语句需要进行多次联表查询才能完成,此时就需要用到存储过程了. 存储过程(Stored Procedure)是 ...
- iOS 仿抖音 视频裁剪
1.最近做短视频拍摄.其中的裁剪界面要做得和抖音的视频裁剪效果一样 需求: 裁剪有一个最大裁剪时间.最小裁剪时间.左右拖动可以实时查看对应的视频画面.拖动进度条也能查看对应的画面 .拖动底部视图也能 ...
- 云计算三种服务模式——IaaS、PaaS和SaaS
云计算的服务模式仍在不断进化,但业界普遍接受将云计算按照服务的提供方式划分为三个大类:SaaS(Software as a Service–软件即服务) PaaS(Platform as a Serv ...
- java----tomcat
下载: 测试使用一般下载zip格式,在服务器上布置tomcat使用直接安装格式(install) https://tomcat.apache.org/download-90.cgi 使用 在windo ...
- 前端angular使用crypto-js进行加密
首先下载大包 npm install crypto-js 然后下载ts版本的包 npm install --save @types/crypto-js 接着在头部导入crypto-js模块 impor ...
- 简单使用zabbix监控nginx是否存活
1.在agent端修改主配置文件 vim /etc/zabbix/zabbix_agentd.conf ........ ........ UserParameter=nginx.status[*], ...
- Django发HTML邮件
1.settings配置 EMAIL_HOST = 'XXXX' DEFAULT_FROM_EMAIL = '张宁 <zhang.ning@XXX.com>' RECEIVER =['zh ...
- TCP和UDP的区别以及使用python服务端客户端简单编程
一.TCP.UDP区别总结 1.TCP面向连接(如打电话要先拨号建立连接):UDP是无连接的,即发送数据之前不需要建立连接 2.TCP提供可靠的服务,也就是说,通过TCP连接传送的数据,无差错,不丢失 ...
- 【工具】Idea GenerateAllSetter
使用工具自动生成setter方法调用,不是idea原生态生成getter/setter https://github.com/gejun123456/intellij-generateAllSetMe ...