首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将HTML标签保存为json格式而不编码

将HTML标签保存为json格式而不编码
EN

Stack Overflow用户
提问于 2020-08-06 16:24:37
回答 1查看 58关注 0票数 0

我正在使用c#生成一个json文件。其中一个属性是文本:“段落文本1

“。当我保存json文件时,文本被转换为:"\u003cp\u003eparagraph text 1\u003c/p\u003e"。我想保留原始文本。我有一个名为Field的实体。它看起来像这样:

代码语言:javascript
运行
复制
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:

代码语言:javascript
运行
复制
"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
}]

下面是我保存文件的方式:

代码语言:javascript
运行
复制
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);
                }

我尝试将文件保存为任何编码格式的文本,但都没有成功。

EN

回答 1

Stack Overflow用户

发布于 2020-08-06 16:38:33

在您的示例中,您可以使用Newtonsoft.Json.JsonConvert类而不是JavaScriptSerializer

代码语言:javascript
运行
复制
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);

结果将是:

代码语言:javascript
运行
复制
{
  "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
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63279607

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档