网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

txt格式转json格式的方法-创新互联

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;


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

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

   public DataCenter()
    {
        List =new List();
    }
}

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 node = JsonMapper.ToObject>(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格式的方法大概了解了吗?希望文章内容对大家有所帮助。如果想了解更多相关文章内容,欢迎关注创新互联行业资讯频道。

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


新闻名称:txt格式转json格式的方法-创新互联
分享URL:http://bjjierui.cn/article/copsjh.html

其他资讯