Сохранить тег HTML в json без кодировки - PullRequest
0 голосов
/ 06 августа 2020

Я создаю файл json, используя c#. Одно из свойств - это текст: «текст абзаца 1

». Когда я сохраняю файл json, текст преобразуется в этот: "\u003cp\u003eparagraph text 1\u003c/p\u003e". Я хочу сохранить оригинальный текст. У меня есть сущность с именем Поле. Это выглядит так:

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

I безуспешно пыталась сохранить файл как текст в любом формате кодировки.

1 Ответ

0 голосов
/ 06 августа 2020

В вашем случае вы можете использовать класс 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
}

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...