How to remove double quotes from json object type JSON value with remove "\r\n \ " tried many times

huangapple go评论51阅读模式
英文:

How to remove double quotes from json object type JSON value with remove "\r\n \ " tried many times

问题

Expected :
[{"MetaId":4,"FilePrefix":" as- apt05 - 04 - 2023", "RequestjView":{ "article": {"article_doi": "10.1139/cgj-2022-44441","data": null},"RequestID": "SCP2304110002"} }]

英文:

Problem :
[{"MetaId":4,"FilePrefix":" as- apt05 - 04 - 2023", "RequestjView":"{ \r\n "article": {\r\n "article_doi": "10.1139/cgj-2022-44441",\r\n "data": null\r\n },\r\n "RequestID": "SCP2304110002"\r\n}"}]

Expected :
[{"MetaId":4,"FilePrefix":" as- apt05 - 04 - 2023", "RequestjView":{ "article": {"article_doi": "10.1139/cgj-2022-44441","data": null},"RequestID": "SCP2304110002"} }]

答案1

得分: 1

如果我们将这些数据存储在一个变量中,那么变量名就是rawData,代码部分如下:

let rawData = '[{"MetaId":4,"FilePrefix":"as-apt05-04-2023","RequestjView":"{ \r\n \"article\": {\r\n \"article_doi\": \"10.1139/cgj-2022-44441\",\r\n \"data\": null\r\n },\r\n \"RequestID\": \"SCP2304110002\"\r\n}"}]';

/* 替换这些字符:\r、\n、\、"{ 和 }" */
rawData = rawData.replace(/\\n/g, '').replace(/\\r/g, '').replace(/\\/g, '').replace(/\"{/g, '{').replace(/\}"/g, '}');

/* 将其转换为对象 */
var dataaF = JSON.parse(rawData);

/* 使用alert可以查看转换后的数据 */
alert(dataaF[0].RequestjView);

只返回代码部分的翻译。

英文:

If we take this data in a variable then variable name is rawData and

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let rawData = &#39;[{&quot;MetaId&quot;:4,&quot;FilePrefix&quot;:&quot; as- apt05 - 04 - 2023&quot;, &quot;RequestjView&quot;:&quot;{ \r\n &quot;article&quot;: {\r\n &quot;article_doi&quot;: &quot;10.1139/cgj-2022-44441&quot;,\r\n &quot;data&quot;: null\r\n },\r\n &quot;RequestID&quot;: &quot;SCP2304110002&quot;\r\n}&quot;}]&#39;;

/* replace these from \r, \n, \, &quot;{ and }&quot; */
rawData = rawData.replace(/\\n/g, &#39;&#39;).replace(/\\r/g, &#39;&#39;).replace(/\\/g, &#39;&#39;).replace(/\&quot;{/g, &#39;{&#39;).replace(/\}&quot;/g, &#39;}&#39;);

/* convert it in object */
var dataaF = JSON.parse(rawData);

/* help of alert you can see the converted data */
alert(dataaF[0].RequestjView);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月24日 18:41:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76322637.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定