簡體   English   中英

如何在 javascript/typescript 中將此字符串轉換為 JSON object?

[英]How to convert this string to JSON object in javascript/typescript?

我一直在嘗試將這段字符串轉換為正確的 JSON obj,經過多次字符串操作和試驗,甚至在使用 JSON.parse 和 stringify 函數之后也無法幫助我。 有人可以幫我把這個字符串轉換為 Proper JSON Obj 嗎?

"{\n  status: 'success',\n  message: 'The user is able to enter data in search bar.'\n}\n{ status: 'success', message: 'Number of cities with letter p are 4.' }\n{\n  status: 'success',\n  message: 'Success messagecity addedappears in green'\n}\n{\n  status: 'success',\n  message: 'The city added is visible under Cities header.'\n}\n"

我嘗試使用 JSON.parse 和 stringify 函數,也嘗試使用 replaceAll、split(),但沒有幫助。

const messageArray = str.replaceAll("\n","").replaceAll("}{","},{").replaceAll('status:', '"status":').replaceAll('message:', '"message":').replaceAll("'", '"').match(/{(.)*?}/g).map(i => JSON.parse(i))

但這是一種可怕的方式。 最好讓這種恥辱的發件人保持正常的看法。

  1. 該字符串在每個 object 中都缺少一些逗號,因此首先要添加這些逗號。
  2. 該字符串是一堆分離的對象,因此在將其轉換為 json 之前,讓我們將它們混合在一起形成一個數組。

 const jsObjecStringWithoutN = document.querySelector('#jsObjecStringWithoutN'); const jsObjecStringAddingCommas = document.querySelector('#jsObjecStringAddingCommas'); const JSONObject = document.querySelector('#JSONObject'); const javascriptObject = "{\n status: 'success',\n message: 'The user is able to enter data in search bar.'\n}\n{ status: 'success', message: 'Number of cities with letter p are 4.' }\n{\n status: 'success',\n message: 'Success messagecity addedappears in green'\n}\n{\n status: 'success',\n message: 'The city added is visible under Cities header.'\n}\n"; // replace the \\n character const replaceEnter = javascriptObject.replaceAll('\n', ''); jsObjecStringWithoutN.innerText = replaceEnter; // add commas at the end of each separated object const addingCommas = replaceEnter.replaceAll('}{', '},{'); jsObjecStringAddingCommas.innerText = addingCommas; //replace single quotes to double quotes const replacingQuotes = addingCommas.replaceAll("\'",'"'); let jsonString = "["+replacingQuotes+"]"; // put double quotes in keys const jsonObjectText = jsonString.replaceAll(/[a-zA-Z0-9]+:/ig, (key)=>{ const formattedKey = key.replace(':',''); return '"'+formattedKey+'":'; }); const jsonObject = JSON.parse(jsonObjectText); JSONObject.innerText = JSON.stringify(jsonObject);
 code{ background: #ececec; }
 <h2>Javascript Object</h1> <code id="jsObjectString">{\n status: 'success',\n message: 'The user is able to enter data in search bar.'\n}\n{ status: 'success', message: 'Number of cities with letter p are 4.' }\n{\n status: 'success',\n message: 'Success messagecity addedappears in green'\n}\n{\n status: 'success',\n message: 'The city added is visible under Cities header.'\n}\n"</code> <h3>First step, delete "/n"</h3> <code id="jsObjecStringWithoutN"></code> <h3>Second step, adding commas</h3> <code id="jsObjecStringAddingCommas"></code> <h3>Third step, wrap it all up in one array of objects and convert it to JSON</h3> <code id="JSONObject"></code>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM