我正在使用c#生成一个json文件。其中一个属性是文本:“段落文本1
“。当我保存json文件时,文本被转换为:"\u003cp\u003eparagraph text 1\u003c/p\u003e"
。我想保留原始文本。我有一个名为Field的实体。它看起来像这样:
public partial class Field
{
[JsonProperty("name")]
public string name { get; set; }
[JsonProperty("identifier")]
public string identifier { get; set; }
[JsonProperty("type")]
public string type { get; set; }
[JsonProperty("isEditorDialogOpen", NullValueHandling = NullValueHandling.Ignore)]
public bool? isEditorDialogOpen { get; set; }
[JsonProperty("paragraphHeight", NullValueHandling = NullValueHandling.Ignore)]
public string paragraphHeight { get; set; }
[JsonProperty("integrationID")]
public string integrationID { get; set; }
[JsonProperty("hint")]
public string hint { get; set; }
[JsonProperty("tooltip")]
public string tooltip { get; set; }
[JsonProperty("validations")]
public object[] validations { get; set; }
[JsonProperty("permission")]
public string permission { get; set; }
[JsonProperty("maskingViewer")]
public string maskingViewer { get; set; }
[JsonProperty("defaultValue")]
public object[] defaultValue { get; set; }
[JsonProperty("width")]
public string width { get; set; }
[JsonProperty("editedParagraph", NullValueHandling = NullValueHandling.Ignore)]
public string editedParagraph { get; set; }
[JsonProperty("label")]
public string label { get; set; }
[JsonProperty("stepIdx")]
public long stepIdx { get; set; }
[JsonProperty("blockIdx")]
public long blockIdx { get; set; }
[JsonProperty("rowIdx")]
public long rowIdx { get; set; }
[JsonProperty("readOnly", NullValueHandling = NullValueHandling.Ignore)]
public bool? readOnly { get; set; }
[JsonProperty("clearable", NullValueHandling = NullValueHandling.Ignore)]
public bool? clearable { get; set; }
[JsonProperty("required", NullValueHandling = NullValueHandling.Ignore)]
public bool? fieldRequired { get; set; }
[JsonProperty("maxLength", NullValueHandling = NullValueHandling.Ignore)]
[JsonConverter(typeof(ParseStringConverter))]
public string maxLength { get; set; }
[JsonProperty("fieldIdx", NullValueHandling = NullValueHandling.Ignore)]
public long? fieldIdx { get; set; }
[JsonProperty("shownInPdfMode", NullValueHandling = NullValueHandling.Ignore)]
public bool? shownInPdfMode { get; set; }
}
这是json:
"fields": [{
"name": "editor.fields.paragraph",
"identifier": "paragraph_kdg3wty6",
"type": "paragraph",
"isEditorDialogOpen": false,
"paragraphHeight": "auto",
"integrationID": "yobi1_a",
"hint": "",
"tooltip": "",
"validations": [],
"permission": "both",
"maskingViewer": "none",
"defaultValue": [],
"width": "full",
"editedParagraph": "\u003cp\u003eparagraph text 1\u003c/p\u003e",
"label": "",
"stepIdx": 0,
"blockIdx": 0,
"rowIdx": 1,
"readOnly": null,
"clearable": null,
"fieldRequired": null,
"maxLength": null,
"fieldIdx": null,
"shownInPdfMode": null
}]
下面是我保存文件的方式:
var json = new JavaScriptSerializer().Serialize(field);
//File.WriteAllText(destination, json);
using (StreamWriter sw = new StreamWriter(File.Open(destination, FileMode.Create), Encoding.Unicode))
{
sw.WriteLine(json);
}
我尝试将文件保存为任何编码格式的文本,但都没有成功。
发布于 2020-08-06 16:38:33
在您的示例中,您可以使用Newtonsoft.Json.JsonConvert
类而不是JavaScriptSerializer
:
var field = new Field() {
editedParagraph = "<p>this is my text</p>"
};
var json = Newtonsoft.Json.JsonConvert.SerializeObject(field,Newtonsoft.Json.Formatting.Indented);
Console.WriteLine(json);
结果将是:
{
"name": null,
"identifier": null,
"type": null,
"integrationID": null,
"hint": null,
"tooltip": null,
"validations": null,
"permission": null,
"maskingViewer": null,
"defaultValue": null,
"width": null,
"editedParagraph": "<p>this is my text</p>",
"label": null,
"stepIdx": 0,
"blockIdx": 0,
"rowIdx": 0
}
https://stackoverflow.com/questions/63279607
复制相似问题