1 回答

TA贡献1757条经验 获得超8个赞
虽然 @str 确实是一个很好的论据,但如果你愿意,你可以这样做:
/**
* Made some modifications to the `escapeSpecialChars` function
*/
const contents = makeJsonData();
// REMOVED
/** const strContents = JSON.stringify(contents); **/
/** const jsonContents = escapeSpecialChars(strContents); **/
const jsonContents = escapeSpecialChars(contents);
// REMOVED
/** const parsedJson = JSON.parse(jsonContents); **/
/** console.log(parsedJson.pages); **/
console.log(jsonContents.pages);
/**
* Helper Functions
*/
function escapeSpecialChars(jsonData = {}) { // <-- Pass in JSON, let the function stringify (as well as parse) the data..
const jsonString = JSON.stringify(jsonData);
const cleanedStr = jsonString.replace(/\n/g, "\\n")
.replace(/\r/g, "\\r")
.replace(/\t/g, "\\t")
.replace(/\f/g, "\\f");
return JSON.parse(cleanedStr);
}
function makeJsonData() { // Helper function to make things a little more legible
return {
"uri": "",
"mimeType": "application/pdf",
"text": "6/17/2020 sample-auto-insurance-declarations-page_1_2x.jpg (1200×1974)\n)\nPolicygenius\nPolicy Number 12345-67-89\nAuto Insurance Declaration Page\n$79848\nFARMERS tt\nPolicy Premium and Fees\nINSURANCE\nPolicy Information\nPremium/Fees\nPolicy Number\nEffective\nExpiration\nNamed Insured(s)\nPolicy Premium\nFees\n$797.60\n$0.88\nPolicy Premium Fees\n$798.48\nAddress\nCity\nUnderwritten by\n12345-67-89\n01/01/2019 12:01 AM\n06/01/2019 12:01 AM\nJack Smith\nJane Smith\n1234 Main St\nBrooklyn, NY 11211\nFarmers Insurance\n6301 Owensmouth Ave.\nWoodland Hills, CA 91367\nTHIS IS NOT A BILL.\nYour bill with the amount due will be mailed separately.\nHousehold Drivers\nAll persons who drive or will occasionally be driving any of the cars on the policy should be listed below. If\nanyone is missing or needs to be added, such as a newly licensed driver, you should contact your agent or\nthe company to add that person before they begin to drive any of the cars covered on the policy.\nName\nJack Smith\nJack Smith Jr.\nDriver Status\nCovered\nCovered\nName\nJane Smith\nDriver Status\nCovered\nVehicle Information\nVeh. #\n1\nYear/Make/Model/VIN\n2019 Porsche Macan 4D 4WD\nWP1A A1234567890\nCoverage\nComprehensive\nCollision\nAdditional Equipment\nDeductible\n$500\n$1,000\nLimit\nACV\nACV\n$1,000\nCoverage Information\nCoverage\nBodily Injury\nProperty Damage\nPermissive User Limit of Liability\nMedical Coverage\nUninsured Motorist Bodily Injury\nLimits\n$250K each person/$500K each incident\n$100K each incident\nFull\n$5,000 each person\n$250K each person/$500K each incident\nVehicle 1\n$301.50\nIncluded\n$17.00\n$66.50\n$44.50\nhttps://images.ctfassets.net/3uw9cov4u60w/6bEiFWZKj35DFMeNXvWsLV/e8e322c241bb5e860f0c26bf9cd8ec8d/sample-auto-insurance-declarations… 1/1\n",
"pages": [{
"pageNumber": 1,
"dimension": {
"width": 612,
"height": 792,
"unit": "points"
}
}]
}
}
添加回答
举报