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

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 .json 文件中將一個鍵的值插入到另一個鍵的值中

在 .json 文件中將一個鍵的值插入到另一個鍵的值中

富國滬深 2022-08-18 15:32:12
這是我的 json 文件{    "foo": "https://3a1821d0.ngrok.io/api/foo",    "bar": "https://3a1821d0.ngrok.io/api/bar",}我想用json文件中的另一個鍵替換,比如3a1821d0{    "some_variable": 3a1821d0,},{    "foo": "https://some_variable.ngrok.io/api/foo",    "bar": "https://some_variable.ngrok.io/api/bar",}這可能嗎?如果是,如何?
查看完整描述

2 回答

?
一只萌萌小番薯

TA貢獻1795條經(jīng)驗 獲得超7個贊

我建議包裝要替換的 ocurrence 或任何其他字符,以便替換不會與外觀發(fā)生沖突。調(diào)用您的文件,這將是它的內(nèi)容:<>input.json


{

    "some_variable": "3a1821d0",

    "foo": "https://<some_variable>.ngrok.io/api/foo",

    "bar": "https://<some_variable>.ngrok.io/api/bar"

}

假設(shè)您正在使用節(jié)點。此代碼應(yīng)該可以完成這項工作。


const fs = require('fs');


let fileContent = fs.readFileSync('input.json', "utf-8");


let content = JSON.parse(fileContent);

const someVariable = content.some_variable;


// I'm adding null and 4 to keep the file beautified

let fileContentStr = JSON.stringify(content, null, 4);


// This line replaces all ocurrences of <some_variable> by "some_variable" content

fileContentStr = fileContentStr.split('<some_variable>').join(someVariable);


// Write file again

fs.writeFileSync('output.json', fileContentStr);


查看完整回答
反對 回復(fù) 2022-08-18
?
阿晨1998

TA貢獻2037條經(jīng)驗 獲得超6個贊

您可以使用以下方法將匹配的內(nèi)容替換為所需的內(nèi)容。替換為所需的文本。test


let data ={

    "foo": "https://3a1821d0.ngrok.io/api/foo",

    "bar": "https://3a1821d0.ngrok.io/api/bar",

}


Object.keys(data).forEach(key => data[key] = data[key].replace(/3a1821d0/,"test"))

console.log(data)


查看完整回答
反對 回復(fù) 2022-08-18
  • 2 回答
  • 0 關(guān)注
  • 121 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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