zoukankan      html  css  js  c++  java
  • Json----简单介绍

    Json

    先分享一个网站http://www.bejson.com/,这个是用来检测Json文件的错误的,Json文件一般不好查找错误.

    看懂Json只需要四句话:

                   对象表示为键值对

    数据由逗号分隔

    花括号保存对象

    方括号保存数组

          

     这就是Json文件.不在过多介绍,重点不在这里

    基础:Json到对象和对象到Json

    需要用到的API:

     JsonMapper.ToJson();

    JsonMapper.ToObject();

    引用命名空间Using LitJson;

    代码:

     1 using System.Collections;
     2 using System.Collections.Generic;
     3 using UnityEngine;
     4 using LitJson;
     5 public class JsonTest : MonoBehaviour {
     6     void Start()
     7     {
     8 //对象到Json
     9         MyIphone myPhone = new MyIphone
    10         {
    11             appNum = 30,
    12             phoneState = true,
    13             appList = new List<string>() { "抖音", "BiliBili", "喜马拉雅听" }
    14         };
    15         string jsonIpone = JsonMapper.ToJson(myPhone);
    16       Debug.Log(jsonIpone);
    17 
    18 
    19 //Json转到对象
    20         string myIphoneJson = "{'appNum':25,'phoneState':false,'appList':['WeChat','Sina','QQ']}";
    21         MyIphone myIPhone = JsonMapper.ToObject<MyIphone>(myIphoneJson);
    22         Debug.Log("手机应用数量:"+myIPhone.appNum+"-手机开机状态"+myIPhone.phoneState);
    23         for (int i = 0; i < myIPhone.appList.Count; i++)
    24         {
    25             Debug.Log(myIPhone.appList[i]);
    26         }
    27     }    
    28 }
    29 public class MyIphone
    30 {
    31     public int appNum;
    32     public bool phoneState;
    33     public List<string> appList;
    34 }

    进阶使用的:

    代码二:

     1 using System.Collections;
     2 using System.Collections.Generic;
     3 using System.IO;
     4 using System.Text;
     5 using LitJson;
     6 using UnityEngine;
     7 public class DataNode// 该class用于json的时候不能有构造函数
     8 {
     9     public string CopyName;
    10     public string CopyPosition;
    11     public string CopyRotation;
    12 }
    13 public class DataCenter
    14 {
    15     public List<DataNode> List;
    16 
    17    public DataCenter()
    18     {
    19         List =new List<DataNode>();
    20     }
    21 }
    22 public class JsonConvert : MonoBehaviour {
    23     private string _txtPath;
    24     private string _jsonPath;  
    25     void Start ()
    26     {
    27         _jsonPath = Application.streamingAssetsPath + "/CopyInfo.json";
    28         _txtPath = Application.streamingAssetsPath + "/CopyInfo.txt";
    29        // Json的解析是很快的 网络
    30         ReadTextToJson();
    31         ReadJsonFromJsonPath();
    32     }
    33     void ReadJsonFromJsonPath()
    34     {
    35       string jsondata =  File.ReadAllText(_jsonPath);
    36         List<DataNode> node = JsonMapper.ToObject<List<DataNode>>(jsondata);
    37         Debug.LogError(node.Count);
    38     }
    39     void ReadTextToJson()
    40     {
    41         DataCenter dc = new DataCenter();
    42         using (StreamReader reader = new StreamReader(_txtPath,Encoding.UTF8))
    43         {
    44             string tmpStr = string.Empty;
    45             while ( !string.IsNullOrEmpty(tmpStr = reader.ReadLine()))
    46             {
    47                 string[] infos = tmpStr.Split('_');
    48                 DataNode _node = new DataNode();
    49                 _node = infos();
    50                 dc.List.Add(_node);
    51             }
    52         }
    53         //数据读取完毕 开始写入json 传递的List<>
    54         string jsonData = JsonMapper.ToJson(dc.List);
    55         File.WriteAllText(_jsonPath,jsonData);
    56     }
    57 }

    代码三:

      1 using System.Collections;
      2 using System.Collections.Generic;
      3 using System.IO;
      4 using System.Text;
      5 using UnityEngine;
      6 using  Excel;
      7 using OfficeOpenXml;
      8 using Excel.Core;
      9 using  System.Data;
     10 using OfficeOpenXml.Style;
     11 using UnityEditor;
     12 using  LitJson;
     13 class CopyInfo  //CopyInfo的构造
     14 {
     15     public string     CopyName;
     16     public Vector3 CopyPosition;
     17     public Quaternion CopyRotation;
     18     public CopyInfo(string name, Vector3 postion, Vector3 rotation)
     19     {
     20         CopyName = name;
     21         CopyPosition = postion;
     22        CopyRotation = Quaternion.Euler(rotation);
     23     }
     24 }
     25 class JsonNode//JsonNode的构造
     26 {
     27     public string StrName;
     28     public string StrPosition;
     29     public string StrRotation;   
     30     public JsonNode(string name,string pos, string rot)
     31     {
     32         StrName = name;
     33         StrPosition = pos;
     34         StrRotation = rot;
     35     }
     36 }
     37 class MyJsonData  //MyJsonData的构造
     38 {
     39     public List<JsonNode> JsonDatalist;
     40 
     41     public MyJsonData()
     42     {
     43         JsonDatalist = new List<JsonNode>();
     44     }
     45 }
     46 public class CreateManager : MonoBehaviour
     47 {
     48     private List<GameObject> _copyList;
     49     private List<CopyInfo>   _copyInfoList;
     50     private MyJsonData _jsonData;
     51     private string _excelPath;
     52     private string _path;
     53     //1. 读文件
     54     //2. 解析文件信息
     55     //3. 实例化
     56         //4. 实例化6个
     57     //4.1 在3s中销毁一个并且添加一个
     58     #region  读取存储在unity的streamingAssets路径中的json
     59     void ReadJsonByJsonPath()
     60     {
     61         string path = Application.streamingAssetsPath + "/CopyInfo.json";//路径
     62         string readInfo =File.ReadAllText(path);//文件读取过程
     63         Debug.LogWarning(readInfo);
     64        
     65         //类型是JsonNode
     66         List<JsonNode> JsonDatalist = new List<JsonNode>();//因为没有new对象!!!
     67         JsonDatalist = JsonMapper.ToObject<List<JsonNode>>(readInfo);//重要代码
     68         Debug.LogError(JsonDatalist.Count);
     69     }
     70   #endregion
     71     void Start()
     72     {
     73         _path = Application.streamingAssetsPath + "/CopyInfo.txt";
     74         _excelPath = Application.streamingAssetsPath + "/CopyInfo.xlsx";
     75         _copyInfoList = new List<CopyInfo>();
     76         _copyList = new List<GameObject>();
     77         _jsonData = new MyJsonData();
     78         WriteJsonByList();
     79         ReadJsonByJsonPath();
     80     }
     81     #region 
     82     private void WriteJsonByList( )
     83     {
     84         string jsonPath = Application.streamingAssetsPath
     85                           + "/CopyInfo.json";
     86         ////将一个list信息转换为json格式 
     87         ////定义一个class 这个class是什么不重要  必须包含一个列表      
     88          _jsonData = new MyJsonData();
     89         using (StreamReader reader  = new StreamReader(_path,Encoding.UTF8))
     90         {
     91             string tmp = string.Empty;
     92             while ( !  string.IsNullOrEmpty(tmp = reader.ReadLine()))
     93             {
     94                 string[] infos = tmp.Split('_');
     95                 _jsonData.JsonDatalist.Add(new JsonNode(infos[0], infos[1], infos[2]));
     96             }
     97         }
     98         //jsondata  数据填充完成
     99         string writeData = JsonMapper.ToJson(_jsonData);//
    100         File.WriteAllText(jsonPath,writeData);
    101     }
    102     #endregion
    103     private void WriteExcel(string path, List<CopyInfo> list)
    104     {
    105         FileInfo excelInfo = new FileInfo(path);
    106         if (excelInfo.Exists)
    107         {
    108             excelInfo.Delete();
    109             excelInfo = new FileInfo(path);
    110         }        
    111         //开始shiyong Excel 
    112         using (ExcelPackage package = new ExcelPackage(excelInfo))
    113         {
    114             ExcelWorksheet sheet = package.Workbook.Worksheets.Add("TestInfo"); // 添加了一个工作表
    115             sheet.Cells[1, 1].Value = "CopyName";
    116             sheet.Cells[1, 2].Value = "CopyPosition";
    117             sheet.Cells[1, 3].Value = "CopyRotation";
    118             for (int i = 0; i < _copyInfoList.Count; i++)
    119             {
    120                 sheet.Cells[2 + i, 1 ].Value = _copyInfoList[i].CopyName;
    121                 sheet.Cells[2 + i, 2 ].Value = _copyInfoList[i].CopyPosition;
    122                 sheet.Cells[2 + i, 3 ].Value = _copyInfoList[i].CopyRotation;
    123             }
    124             sheet.Cells.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
    125             sheet.Cells.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
    126             sheet.Cells.Style.Font.Bold = true;
    127             sheet.Cells.Style.Font.Name = "宋体";
    128             sheet.Cells.Style.Font.Size = 28;
    129             sheet.Cells.AutoFitColumns(50, 150);
    130             package.Save();
    131         }
    132         AssetDatabase.Refresh();
    133     }
    134 }
  • 相关阅读:
    python 矩阵转置
    go
    Go-GRPC 初体验
    SpringMVC笔记——Spring+MyBatis组合开发简单实例
    MyBatis笔记——EhCache二级缓存
    MyBatis笔记——Mapper动态代理
    MyBatis笔记——初次环境配置
    Spring笔记——配置Hibernate框架事务
    Hibernate笔记——关联关系配置(一对多、一对一和多对多)
    Mysql笔记——触发器简单实例
  • 原文地址:https://www.cnblogs.com/Future-Better/p/10028314.html
Copyright © 2011-2022 走看看