为了账号安全,请及时绑定邮箱和手机立即绑定

访问 JSON 值时出现“未定义”

访问 JSON 值时出现“未定义”

红糖糍粑 2022-10-27 15:09:40
我正在尝试访问我的 JSON 中的“页面”值,但试图获得一个“未定义”值。我的代码var fs = require('fs'); var contents = fs.readFileSync('jsonformatter.json', 'utf8');var jsonContents = JSON.stringify(contents);function escapeSpecialChars(jsonString) {    return jsonString.replace(/\n/g, "\\n")        .replace(/\r/g, "\\r")        .replace(/\t/g, "\\t")        .replace(/\f/g, "\\f");}escapeSpecialChars(jsonContents)var jsonContents = JSON.parse(jsonContents);console.log(jsonContents.pages);我的 JSON 的一个片段(将包括所有,但它的 20,000 + 行){"uri": "",  "mimeType": "application/pdf",  "pages": [    {      "pageNumber": 1,      "dimension": {        "width": 612,        "height": 792,        "unit": "points"      }    }  ]}提前感谢您的帮助!
查看完整描述

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"

      }

    }]

  }

}


查看完整回答
反对 回复 2022-10-27
  • 1 回答
  • 0 关注
  • 179 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号