using Microsoft.Win32; using Newtonsoft.Json; using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using static MPTxtToJson.JsonModel; using static MPTxtToJson.JsonModel.QuestAss; namespace MPTxtToJson { /// /// MainWindow.xaml 的交互逻辑 /// public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private string[] strArr; private string jsonStr; /// /// 导入mp航点文件 按钮事件 /// /// /// private void HandleImportTxt(object sender, RoutedEventArgs e) { bool isSelect;//判断有没有获取到文件名 string fileName = this.OpExplorer("航点文件导入",out isSelect);//获取航点文件路径和文件名 if (isSelect) { strArr = this.ReadFile(fileName); this.JsonButton.IsEnabled = true;//激活导出json按钮 this.jsonStr=JsonBase.ModelTojson(this.MakeJsonModel());//得到json字符串 } } /// /// 保存转换好的json到文件 /// /// /// private void HandleSaveJsonFile(object sender, RoutedEventArgs e) { SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "航点文件|*.json;";//资源管理器限制扩展名 sfd.Title = "保存json航点文件"; sfd.AddExtension = true;//设置自动在文件名中添加扩展名 if (sfd.ShowDialog() == true) { this.SaveStrToFile(sfd.FileName, this.jsonStr); } } /// /// 生成json模型 /// /// private JsonModel MakeJsonModel() { JsonModel jsonModel = new JsonModel(); QuestAss questAss = new QuestAss(); questAss.ID = "FliCube"; questAss.ver = "1.0"; questAss.taskcount = this.strArr.Length-1; List tasks = new List(); int i = 0; foreach (var item in strArr) { if (i > 0) { //把每一行的航点字符串 用tab分割出每个参数 char[] separator = {'\t'}; string[] sArr = item.Split(separator); Tasks t = new Tasks(); t.seq = Convert.ToInt32(sArr[0]); t.frame = Convert.ToInt32(sArr[2]); t.command = Convert.ToInt32(sArr[3]); t.current = 0; t.autocontinue = 1; t.param1 = Convert.ToDouble(sArr[4]); t.param2 = Convert.ToDouble(sArr[5]); t.param3 = Convert.ToDouble(sArr[6]); t.param4 = Convert.ToDouble(sArr[7]); t.x = Convert.ToDouble(sArr[8]); t.y = Convert.ToDouble(sArr[9]); t.z = Convert.ToDouble(sArr[10]); if (t.command == 94) { t.sound = this.soundField.Text; } tasks.Add(t); } i++; } questAss.tasks = tasks; jsonModel.questAss = questAss; return jsonModel; } /// /// 资源管理器 获取文件路径及文件名 /// /// 窗口名称 /// 返回 有没有获取到文件 /// 返回路径及文件名 private string OpExplorer(string titName, out bool isSelect) { Microsoft.Win32.OpenFileDialog ofdl = new Microsoft.Win32.OpenFileDialog();//打开资源管理器 ofdl.Filter = "航点文件|*.txt;";//资源管理器限制扩展名 ofdl.Title = titName; if (ofdl.ShowDialog() == true)//执行打开资源管理器 并判断有没有获取到文件名 { isSelect = true; } else { isSelect = false; } return ofdl.FileName; } /// /// 逐行读取 文档 /// /// 文件路径 /// 返回每行的字符串 集合 private string[] ReadFile(string path) { StreamReader sr = new StreamReader(path, Encoding.Default); String line; ArrayList arrlist = new ArrayList(); while ((line = sr.ReadLine()) != null) { arrlist.Add(line); } if (arrlist.Count == 1)//判断 svg的文件内容 是否写在一行里面 { string temp = (string)arrlist[0]; string[] strArray = Regex.Split(temp, "/>"); return strArray; } string[] arr = new string[arrlist.Count]; int key = 0; foreach (var item in arrlist) { arr[key] = (string)item; key++; } return arr; } /// /// 字符串 保存到指定文件 /// /// 指定文件和路径 /// 字符串 private void SaveStrToFile(string filePath, string str) { str = str.TrimEnd((char[])"\n\r".ToCharArray());//去除最后的回车符 string stream = null; if (File.Exists(filePath)) { StreamReader reader = new StreamReader(filePath); stream = reader.ReadToEnd(); reader.Close(); } FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate); StreamWriter sw = new StreamWriter(fs); sw.Write(str); sw.Close(); fs.Close(); } } /// /// json文件的数据结构 /// public class JsonModel { public QuestAss questAss { get; set; } public class QuestAss { public string ID { get; set; } public string ver { get; set; } public int taskcount { get; set; } public List tasks { get; set; } public class Tasks { public int seq { get; set; }//航点序号 public int frame { get; set; } public int command { get; set; } public int current { get; set; } public int autocontinue { get; set; } public double param1 { get; set; } public double param2 { get; set; } public double param3 { get; set; } public double param4 { get; set; } public double x { get; set; } public double y { get; set; } public double z { get; set; } [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] //Newtonsoft.Json 库中的一个属性。这个属性用于控制对象在序列化为 JSON 时如何处理 null 值, null值 就不添加这个字段 public string sound { get; set; } // 新增的 sound 字段 } } } public static class JsonBase { /// /// FcgmJsonModel对象 序列化 成 json /// /// /// public static string ModelTojson(JsonModel jsonModel) { return JsonConvert.SerializeObject(jsonModel);    //转为字符串 } } }