如何将包含编码 HTML 的 json 字符串反序列化为 JObject?
How to deserialize a json string containing encoded HTML into JObject?
我有一个包含编码 HTML 的 json 字符串,如下所示,这是我在执行 Shopify Liquid 转义后得到的。我正在尝试解码内部 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"
}
实际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"
}
但是,当我尝试解码和反序列化时,它失败了。我的代码是:
string test = "{\r\n\t\"testId\": 494254,\r\n\t\"languageIdentifier\": \"en_us\",\r\n\t\"overview\":\"<p style="margin: 0px;"><span>Overview'ff' Test</span></p>\",\t\r\n\t\"responsibilities\":\"<p style="margin: 0px;"><span>Responsibilities</span></p>\",\r\n\t\"qualifications\":\"<p style="margin: 0px;"><span>Qualifications</span></p>\",\r\n\t\"guidance\":\"z_used_Guidance Test\",\r\n\t\"additionalDetailsForInternalCandidates\":\"<p style="margin: 0px;"><span>Additional Details</span></p>\",\r\n\t\"requisitionNotes\":\"Requisition Notes\"}";
var jsonString = HttpUtility.HtmlDecode(test);
var objectjson = JsonConvert.DeserializeObject<JObject>(jsonString);
但是,我得到这个错误:
'After parsing a value an unexpected character was encountered: m. Path 'overview', line 4, position 23.'
谁能帮我解码编码的 HTML 并将其反序列化为 JObject?
我希望它像原始输入
一样作为解码后的 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"
}
提前致谢。
您必须按照以下 C# 代码执行操作
图书馆
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
代码
string test = "{\r\n\t\"testId\": 494254,\r\n\t\"languageIdentifier\": \"en_us\",\r\n\t\"overview\":\"<p style="margin: 0px;"><span>Overview Test</span></p>\",\t\r\n\t\"responsibilities\":\"<p style="margin: 0px;"><span>Responsibilities</span></p>\",\r\n\t\"qualifications\":\"<p style="margin: 0px;"><span>Qualifications</span></p>\",\r\n\t\"guidance\":\"z_used_Guidance Test\",\r\n\t\"additionalDetailsForInternalCandidates\":\"<p style="margin: 0px;"><span>Additional Details</span></p>\",\r\n\t\"requisitionNotes\":\"Requisition Notes\"}";
JToken objectData = JToken.Parse(test);
var testId = objectData["testId"];
var languageIdentifier = objectData["languageIdentifier"];
如果您想从 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\":\"<p style="margin: 0px;"><span>Overview Test</span></p>\",\t\r\n\t\"responsibilities\":\"<p style="margin: 0px;"><span>Responsibilities</span></p>\",\r\n\t\"qualifications\":\"<p style="margin: 0px;"><span>Qualifications</span></p>\",\r\n\t\"guidance\":\"z_used_Guidance Test\",\r\n\t\"additionalDetailsForInternalCandidates\":\"<p style="margin: 0px;"><span>Additional Details</span></p>\",\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>
您假设您有一个HTML编码的JSON文件。事实并非如此。您拥有的是 一个包含 HTML 编码值 的 JSON 文件。那是不一样的。
意味着你先需要解析JSON然后然后HTML-解码值.
string jsonString = @"{
""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""
}";
var objectjson = JsonConvert.DeserializeObject<JObject>(jsonString);
var htmlEncodedValue = objectjson.Value<string>("overview");
var decodedValue = HttpUtility.HtmlDecode(htmlEncodedValue);
我有一个包含编码 HTML 的 json 字符串,如下所示,这是我在执行 Shopify Liquid 转义后得到的。我正在尝试解码内部 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"
}
实际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"
}
但是,当我尝试解码和反序列化时,它失败了。我的代码是:
string test = "{\r\n\t\"testId\": 494254,\r\n\t\"languageIdentifier\": \"en_us\",\r\n\t\"overview\":\"<p style="margin: 0px;"><span>Overview'ff' Test</span></p>\",\t\r\n\t\"responsibilities\":\"<p style="margin: 0px;"><span>Responsibilities</span></p>\",\r\n\t\"qualifications\":\"<p style="margin: 0px;"><span>Qualifications</span></p>\",\r\n\t\"guidance\":\"z_used_Guidance Test\",\r\n\t\"additionalDetailsForInternalCandidates\":\"<p style="margin: 0px;"><span>Additional Details</span></p>\",\r\n\t\"requisitionNotes\":\"Requisition Notes\"}";
var jsonString = HttpUtility.HtmlDecode(test);
var objectjson = JsonConvert.DeserializeObject<JObject>(jsonString);
但是,我得到这个错误:
'After parsing a value an unexpected character was encountered: m. Path 'overview', line 4, position 23.'
谁能帮我解码编码的 HTML 并将其反序列化为 JObject? 我希望它像原始输入
一样作为解码后的 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"
}
提前致谢。
您必须按照以下 C# 代码执行操作
图书馆
using Newtonsoft.Json; using Newtonsoft.Json.Linq;
代码
string test = "{\r\n\t\"testId\": 494254,\r\n\t\"languageIdentifier\": \"en_us\",\r\n\t\"overview\":\"<p style="margin: 0px;"><span>Overview Test</span></p>\",\t\r\n\t\"responsibilities\":\"<p style="margin: 0px;"><span>Responsibilities</span></p>\",\r\n\t\"qualifications\":\"<p style="margin: 0px;"><span>Qualifications</span></p>\",\r\n\t\"guidance\":\"z_used_Guidance Test\",\r\n\t\"additionalDetailsForInternalCandidates\":\"<p style="margin: 0px;"><span>Additional Details</span></p>\",\r\n\t\"requisitionNotes\":\"Requisition Notes\"}"; JToken objectData = JToken.Parse(test); var testId = objectData["testId"]; var languageIdentifier = objectData["languageIdentifier"];
如果您想从 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\":\"<p style="margin: 0px;"><span>Overview Test</span></p>\",\t\r\n\t\"responsibilities\":\"<p style="margin: 0px;"><span>Responsibilities</span></p>\",\r\n\t\"qualifications\":\"<p style="margin: 0px;"><span>Qualifications</span></p>\",\r\n\t\"guidance\":\"z_used_Guidance Test\",\r\n\t\"additionalDetailsForInternalCandidates\":\"<p style="margin: 0px;"><span>Additional Details</span></p>\",\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>
您假设您有一个HTML编码的JSON文件。事实并非如此。您拥有的是 一个包含 HTML 编码值 的 JSON 文件。那是不一样的。
意味着你先需要解析JSON然后然后HTML-解码值.
string jsonString = @"{
""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""
}";
var objectjson = JsonConvert.DeserializeObject<JObject>(jsonString);
var htmlEncodedValue = objectjson.Value<string>("overview");
var decodedValue = HttpUtility.HtmlDecode(htmlEncodedValue);