txt转json


using System.Collections.Generic;
using System.IO;
using System.Text;
using LitJson;
using UnityEngine;


/// <summary>
/// 该class用于json的时候不能有构造函数
/// </summary>
public class DataNode
{
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();
}

// 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();
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);
}
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM