首页 >web前端 >js教程 > 正文

txt格式怎么转json格式

原创2020-09-17 11:09:4207333

txt转json格式的方法:首先打开相应的代码脚本文件;然后通过【ReadTextToJson();】方法读取TXT文件并转化为Json即可。

JSON是一种轻量级的数据交换格式。它基于 ECMAScript 的一个子集,采用完全独立于编程语言的文本格式来存储和表示数据。简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。 易于人阅读和编写,同时也易于机器解析和生成,并有效地提升网络传输效率。

以下是转换的程序代码:

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using LitJson;
using UnityEngine;
using Excel;
using Excel.Core;
using OfficeOpenXml.Style;
using OfficeOpenXml;


/// <summary>
/// 该class用于json的时候不能有构造函数
/// </summary>
public class DataNode//自定义类来承接一会读出来的数据分类
{
    public string CopyName;
    public string CopyPosition;
    public string CopyRotation;
}

public class DataCenter//自定义类包含List列表来添加一会读取出来的的数据信息
{
    public List<DataNode> List;

   public DataCenter()
    {
        List =new List<DataNode>();
    }
}

public class JsonConvert : MonoBehaviour {

	// Use this for initialization
    private string _txtPath;//TXT文件路径
    private string _jsonPath;//转换后写入的json路径
    private string _excelPath;
    
	void Start ()
	{
        _jsonPath = Application.streamingAssetsPath + "/CopyInfo.json";//定义路径
	    _txtPath = Application.streamingAssetsPath + "/CopyInfo.txt";
        _excelPath = Application.streamingAssetsPath + "/CopyInfo.json";
	   // Json的解析是很快的 网络
        ReadTextToJson();//读取TXT文件并转化为Json
	    ReadJsonFromJsonPath();//读取Json文件
        WriteExcel(_excelPath);

    }

	// Update is called once per frame
	void Update () {
		
	}

    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();//实例化dc,待会用其List
        //读文件固定格式
        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.CopyName = infos[0];//把读取的内容赋值
                _node.CopyPosition = infos[1];
                _node.CopyRotation = infos[2];
                dc.List.Add(_node);//把内容添加进列表
            }
        }
        //数据读取完毕 开始写入json 传递的List<>
        string jsonData = JsonMapper.ToJson(dc.List);
        File.WriteAllText(_jsonPath,jsonData);

    }
    private void WriteExcel(string path)
    {
        DataCenter dc = new DataCenter();//实例化dc,待会用其List
        //读文件固定格式
        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.CopyName = infos[0];//把读取的内容赋值
                _node.CopyPosition = infos[1];
                _node.CopyRotation = infos[2];
                dc.List.Add(_node);//把内容添加进列表
            }
        }
        Debug.LogError(dc.List.Count);
        FileInfo excelInfo = new FileInfo(path);
        if (excelInfo.Exists)
        {
            excelInfo.Delete();
            excelInfo = new FileInfo(path);

        }

        //开始使用 Excel 
        using (ExcelPackage package = new ExcelPackage(excelInfo))
        {
            ExcelWorksheet sheet = package.Workbook.Worksheets.Add("TestInfo"); // 添加了一个工作表
            sheet.Cells[1, 1].Value = "CopyName";
            sheet.Cells[1, 2].Value = "CopyPosition";
            sheet.Cells[1, 3].Value = "CopyRotation";

            for (int i = 0; i < dc.List.Count; i++)
            {
                sheet.Cells[2 + i, 1].Value = dc.List[i].CopyName;
                sheet.Cells[2 + i, 2].Value = dc.List[i].CopyPosition;
                sheet.Cells[2 + i, 3].Value = dc.List[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 = 28;
            sheet.Cells.AutoFitColumns(50, 150);

            package.Save();
        }
        
    }
}

以上就是txt格式怎么转json格式的详细内容,更多请关注php中文网其它相关文章!

php中文网最新课程二维码

声明:本文原创发布php中文网,转载请注明出处,感谢您的尊重!如有疑问,请联系admin@php.cn处理

  • 相关标签:json txt
  • 相关文章

    相关视频


    网友评论

    文明上网理性发言,请遵守 新闻评论服务协议

    我要评论
  • 专题推荐

    作者信息

    little bottle

    认证0级讲师

    最近文章
    mysql免费版好用么3460
    mysql怎么输入3100
    怎么看mysql安装路径4520
    推荐视频教程
  • JSON中文手册JSON中文手册
  • FastJson教程手册FastJson教程手册
  • JSON中文手册JSON中文手册
  • AJAX跨域解决方案:JSONP视频教程AJAX跨域解决方案:JSONP视频教程
  • 视频教程分类