第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

訪問 JSON 值時(shí)出現(xiàn)“未定義”

訪問 JSON 值時(shí)出現(xiàn)“未定義”

紅糖糍粑 2022-10-27 15:09:40
我正在嘗試訪問我的 JSON 中的“頁面”值,但試圖獲得一個(gè)“未定義”值。我的代碼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 的一個(gè)片段(將包括所有,但它的 20,000 + 行){"uri": "",  "mimeType": "application/pdf",  "pages": [    {      "pageNumber": 1,      "dimension": {        "width": 612,        "height": 792,        "unit": "points"      }    }  ]}提前感謝您的幫助!
查看完整描述

1 回答

?
陪伴而非守候

TA貢獻(xiàn)1757條經(jīng)驗(yàn) 獲得超8個(gè)贊

雖然 @str 確實(shí)是一個(gè)很好的論據(jù),但如果你愿意,你可以這樣做:


/**

 * 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"

      }

    }]

  }

}


查看完整回答
反對(duì) 回復(fù) 2022-10-27
  • 1 回答
  • 0 關(guān)注
  • 197 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)