/// <summary> /// 读取指定的json文本 /// </summary> /// <param name="info">FileInfo格式文件信息</param> /// <param name="context">ref的参数位置</param> /// <returns></returns> public static bool ReadJson(FileInfo info, ref RefData context) { try { if (context == null) { context = new RefData(); } StreamReader s = info.OpenText(); JsonTextReader reader = new JsonTextReader(s); JObject jObject = (JObject)JToken.ReadFrom(reader); //设置参考版本 context.Version = (int)jObject["version"]; //读取并添加所有翻译参考文本 context.RefTranSlation = JsonConvert.DeserializeObject <Dictionary <string, ObservableCollection <DataBlock> > >(Regex.Replace(jObject["translation"].Value <Object>().ToString(), Environment.NewLine, "")); //读取并添加所有模板参考文本 context.RefTemplate = JsonConvert.DeserializeObject <ObservableCollection <DataBlock> > (Regex.Replace(jObject["template"].Value <Object>().ToString(), Environment.NewLine, "")); //读取并添加所有提示建议文本 context.RefNotice = JsonConvert.DeserializeObject <ObservableCollection <MyString> >(Regex.Replace(jObject["notice"].Value <Object>().ToString(), Environment.NewLine, "")); s.Close(); return(true); } catch { context = null; return(false); } }
public static void SaveJson(FileInfo info, RefData context) { try { if (context == null) { return; } StreamWriter f = new StreamWriter(info.Create(), Encoding.UTF8); JObject jObject = new JObject(); jObject.Add("version", context.Version);//添加当前版本 string serializeJson = JsonConvert.SerializeObject(context.RefTranSlation); jObject.Add("translation", serializeJson); serializeJson = JsonConvert.SerializeObject(context.RefTemplate); jObject.Add("template", serializeJson); serializeJson = JsonConvert.SerializeObject(context.RefNotice); jObject.Add("notice", serializeJson); string temp = jObject.ToString(); f.Write(temp); f.Close(); } catch { return; } }
/// <summary> /// 双击选择要读取的参考JSON /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void RefGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e) { if (sender != null) { DataGrid grid = sender as DataGrid; if (grid != null && grid.SelectedItems != null && grid.SelectedItems.Count == 1) { FileData info = (FileData)grid.SelectedItem; RefData rd = new RefData(); this.refDataGrid.DataContext = rd; if (!FileOperator.ReadJson(info.Fdata, ref rd)) { MessageBoxResult result = MessageBox.Show("参考文本读取失败!", "警告", MessageBoxButton.OK, MessageBoxImage.Error); return; } this.refgridCBox.SelectedIndex = 0; this.addBtn.IsEnabled = true; this.dunBtn.IsEnabled = true; } } }