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提供一个具有高效的协议数据交换格式工具库( ...
随机推荐
- CentOs7.5安装PostgreSQL11
前言 本章介绍在CentOs上安装一个PostgreSQL数据库 下一篇可能是安装 Redis 本篇使用的服务器是已经安装过Python/Nginx等常用软件的环境,因此在安装过程中可能会遇到按照本章 ...
- 面向对象之组合VS继承:继承过时了?
在阅读Effective Java中的第16条时发现了一个有趣的机制或者说是模式,那就是组合(文中翻译为复用,但是作者认为组合更能体现这种模式的精神),并且文中建议使用组合. 那什么是组合, ...
- 小程序 movable-view 在页面中的可移动图标
项目中需要一个可拖动的小图标, 1.小程序组件movable-view 文档地址:https://developers.weixin.qq.com/miniprogram/dev/component/ ...
- 【彻底解决】django migrate (mysql.W002) 【专治强迫症】
cmd中使用python3 manage.py migrate命令,报warn,很多人都遇到过 解决办法: settings.py文件夹加入DATABASES['OPTIONS']['init_com ...
- Java 批量修改文件夹里面的文件的名字
背景:公司要求使用PADS完成原理图设计.PCB Layout.而他硬件工程师要求我在将PADS的库文件发送给他们之前,必须在每一个库文件的后面追加今天的日期,再发送给他们. 问题来了,如果一次需要发 ...
- CollectionUtils工具类的常用方法
集合判断: 例1: 判断集合是否为空: CollectionUtils.isEmpty(null): true CollectionUtils.isEmpty(new ArrayList()): t ...
- .net core 利用日志查看ef生成的SQL语句
EF Core 没有直接提供像 EF6 那样方便的在日志中记录最终生成的 SQL 的功能,可以通过官方提供的日志记录(Microsoft.Extensions.Logging)实现. 一. 使用 Mi ...
- word2vec并行实现小记
word2vec能将文本中出现的词向量化,其原理建立在Mikolov的博士论文成果及其在谷歌的研究经验的基础上.与潜在语义分析(Latent Semantic Index, LSI).潜在狄立克雷分配 ...
- 使用wget命令下载网络资源
wget是GNU/Linux下的一个非交互式(non-interactive)网络下载工具,支持HTTP.HTTPS与FTP协议,并能够指定HTTP代理服务器.虽然wget命令与curl命令相比支持的 ...
- HashMap 学习 (JDK8)
1.hashmap中hash函数的实现中,异或运算操作的结果是什么,为什么要做这样的异或运算 static final int hash(Object key) { int h; return (ke ...