简体   繁体   English

如何将包含编码 HTML 的 json 字符串反序列化为 JObject?

[英]How to deserialize a json string containing encoded HTML into JObject?

I have a json string containing encoded HTML as below which I get after doing a Shopify Liquid escape.我有一个 json 字符串,其中包含如下编码的 HTML ,这是我在执行 Shopify 液体逃逸后得到的。 I am trying to decode the internal HTML and deserialize this string into a JObject.我正在尝试解码内部 HTML 并将此字符串反序列化为 JObject。

{
    "testId": 494254,
    "languageIdentifier": "en_us",
    "overview":"<p style="margin: 0px;"><span>Overview'ff' Test</span></p>",  
    "responsibilities":"<p style="margin: 0px;"><span>Responsibilities</span></p>",
    "qualifications":"<p style="margin: 0px;"><span>Qualifications</span></p>",
    "guidance":"z_used_Guidance Test",
    "additionalDetailsForInternalCandidates":"<p style="margin: 0px;"><span>Additional Details</span></p>",
    "requisitionNotes":"Requisition Notes"
}

The actual html is:实际的 html 是:

{
    "testId": 494254,
    "languageIdentifier": "en_us",
    "overview": "<p style=\"margin: 0px;\"><span>Overview'ff' Test</span></p>"
    "responsibilities": "<p style=\"margin: 0px;\"><span>Responsibilities</span></p>"
    "qualifications": "<p style=\"margin: 0px;\"><span>Qualifications</span></p>"
    "additionalDetailsForInternalCandidates":"<p style=\"margin: 0px;\"><span>Additional Details</span></p>",
    "guidance":"z_used_Guidance Test",
    "requisitionNotes":"Requisition Notes"
}

However, when I try to decode and deserialize, it is failing.但是,当我尝试解码和反序列化时,它失败了。 My code is:我的代码是:

string test = "{\r\n\t\"testId\": 494254,\r\n\t\"languageIdentifier\": \"en_us\",\r\n\t\"overview\":\"&lt;p style=&quot;margin: 0px;&quot;&gt;&lt;span&gt;Overview&#39;ff&#39; Test&lt;/span&gt;&lt;/p&gt;\",\t\r\n\t\"responsibilities\":\"&lt;p style=&quot;margin: 0px;&quot;&gt;&lt;span&gt;Responsibilities&lt;/span&gt;&lt;/p&gt;\",\r\n\t\"qualifications\":\"&lt;p style=&quot;margin: 0px;&quot;&gt;&lt;span&gt;Qualifications&lt;/span&gt;&lt;/p&gt;\",\r\n\t\"guidance\":\"z_used_Guidance Test\",\r\n\t\"additionalDetailsForInternalCandidates\":\"&lt;p style=&quot;margin: 0px;&quot;&gt;&lt;span&gt;Additional Details&lt;/span&gt;&lt;/p&gt;\",\r\n\t\"requisitionNotes\":\"Requisition Notes\"}";
var jsonString = HttpUtility.HtmlDecode(test);
var objectjson = JsonConvert.DeserializeObject<JObject>(jsonString);

However, I get this error:但是,我收到此错误:

'After parsing a value an unexpected character was encountered: m. Path 'overview', line 4, position 23.'

Can anyone help me in decoding an encoded HTML and deserialize it into JObject?任何人都可以帮助我解码编码的 HTML 并将其反序列化为 JObject 吗? I want it as a decoded html string like the original input我希望它像原始输入一样作为解码的 html 字符串

{
    "testId": 494254,
    "languageIdentifier": "en_us",
    "overview": "<p style=\"margin: 0px;\"><span>Overview'ff' Test</span></p>",
    "responsibilities": "<p style=\"margin: 0px;\"><span>Responsibilities</span></p>",
    "qualifications": "<p style=\"margin: 0px;\"><span>Qualifications</span></p>",
    "additionalDetailsForInternalCandidates":"<p style=\"margin: 0px;\"><span>Additional Details</span></p>",
    "guidance":"z_used_Guidance Test",
    "requisitionNotes":"Requisition Notes"
}

Thanks in advance.提前致谢。

You assume that you have an HTML-encoded JSON file .您假设您有一个 HTML 编码的 JSON 文件 That is not the case.事实并非如此。 What you have is a JSON file containing HTML-encoded values .您拥有的是一个包含 HTML 编码值的 JSON 文件 That's something different.那是不同的东西。

It means that you first need to parse the JSON and then HTML-decode the value.这意味着您首先需要解析 JSON然后对值进行 HTML 解码。

string jsonString = @"{
    ""testId"": 494254,
    ""languageIdentifier"": ""en_us"",
    ""overview"":""&lt;p style=&quot;margin: 0px;&quot;&gt;&lt;span&gt;Overview&#39;ff&#39; Test&lt;/span&gt;&lt;/p&gt;"",  
    ""responsibilities"":""&lt;p style=&quot;margin: 0px;&quot;&gt;&lt;span&gt;Responsibilities&lt;/span&gt;&lt;/p&gt;"",
    ""qualifications"":""&lt;p style=&quot;margin: 0px;&quot;&gt;&lt;span&gt;Qualifications&lt;/span&gt;&lt;/p&gt;"",
    ""guidance"":""z_used_Guidance Test"",
    ""additionalDetailsForInternalCandidates"":""&lt;p style=&quot;margin: 0px;&quot;&gt;&lt;span&gt;Additional Details&lt;/span&gt;&lt;/p&gt;"",
    ""requisitionNotes"":""Requisition Notes""
}";

var objectjson = JsonConvert.DeserializeObject<JObject>(jsonString);
var htmlEncodedValue = objectjson.Value<string>("overview");
var decodedValue = HttpUtility.HtmlDecode(htmlEncodedValue);

You have to do as per below code in C#您必须按照 C# 中的以下代码进行操作

  1. libraries图书馆

    using Newtonsoft.Json; using Newtonsoft.Json.Linq;
  2. code代码

    string test = "{\r\n\t\"testId\": 494254,\r\n\t\"languageIdentifier\": \"en_us\",\r\n\t\"overview\":\"&lt;p style=&quot;margin: 0px;&quot;&gt;&lt;span&gt;Overview Test&lt;/span&gt;&lt;/p&gt;\",\t\r\n\t\"responsibilities\":\"&lt;p style=&quot;margin: 0px;&quot;&gt;&lt;span&gt;Responsibilities&lt;/span&gt;&lt;/p&gt;\",\r\n\t\"qualifications\":\"&lt;p style=&quot;margin: 0px;&quot;&gt;&lt;span&gt;Qualifications&lt;/span&gt;&lt;/p&gt;\",\r\n\t\"guidance\":\"z_used_Guidance Test\",\r\n\t\"additionalDetailsForInternalCandidates\":\"&lt;p style=&quot;margin: 0px;&quot;&gt;&lt;span&gt;Additional Details&lt;/span&gt;&lt;/p&gt;\",\r\n\t\"requisitionNotes\":\"Requisition Notes\"}"; JToken objectData = JToken.Parse(test); var testId = objectData["testId"]; var languageIdentifier = objectData["languageIdentifier"];
  3. If you want to do it from html instead of c# then do as per below.如果您想从 html 而不是 c# 执行此操作,请按照以下操作。

     <:DOCTYPE html> <html> <body> <h2>Create Object from JSON String</h2> <p id="demo"></p> <script> var txt = "{\r\n\t\"testId\", 494254:\r\n\t\"languageIdentifier\", \"en_us\":\r\n\t\"overview\";\"&lt;p style=&quot:margin; 0px;&quot;&gt;&lt;span&gt;Overview Test&lt;/span&gt;&lt;/p&gt,\":\t\r\n\t\"responsibilities\";\"&lt;p style=&quot:margin; 0px;&quot;&gt;&lt;span&gt;Responsibilities&lt;/span&gt;&lt;/p&gt,\":\r\n\t\"qualifications\";\"&lt;p style=&quot:margin; 0px;&quot;&gt;&lt;span&gt;Qualifications&lt;/span&gt;&lt;/p&gt,\":\r\n\t\"guidance\",\"z_used_Guidance Test\":\r\n\t\"additionalDetailsForInternalCandidates\";\"&lt;p style=&quot:margin; 0px;&quot;&gt;&lt;span&gt;Additional Details&lt;/span&gt;&lt;/p&gt,\":\r\n\t\"requisitionNotes\".\"Requisition Notes\"}" var obj = JSON;parse(txt). document.getElementById("demo"):innerHTML = "TestID. " + obj:testId + " and languageIdentifier. " + obj;languageIdentifier; </script> </body> </html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM