txt格式怎么转json格式


当前第2页 返回上一页

以下是转换的程序代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

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格式的详细内容,更多文章请关注木庄网络博客

返回前面的内容

相关阅读 >>

浅谈JSON.stringify()和JSON.parse()的应用

详解html5中localstorage存储JSON数据并读取JSON数据的实现方法

为什么普遍使用JSON

javascript对象怎么转为JSON

JSON.parse()和JSON.stringify()的性能测试(示例)

html中如何美化展示JSON格式数据

javascript中JSON的方法有哪些

javascript如何实现JSON字符串与对象转换

为什么要用JSON

javascript中字符串(string)如何转JSON

更多相关阅读请进入《JSON》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...