我想将新元素添加到json对象的特定索引/位置。
如果我使用async function someFunction() {
// Imports the Google Cloud client library
const {Translate} = require('@google-cloud/translate');
// Instantiates a client
const translate = new Translate({projectId});
// The text to translate
const text = 'Hello, world!';
// The target language
const target = 'fr';
// Translates some text into French
const [translation] = await translate.translate(text, target);
console.log(`Text: ${text}`);
console.log(`Translation: ${translation}`);
}
,它将添加到json对象的末尾。
data["country"] = "value"
我希望国家/地区值位于下面的输出中。
import json
data = json.loads('''{"user_name": "xcv","password": "dsjvwebv","age":27,"address":{"country_name": "value",
"state_name":"tamil nadu", "district" :"Tirunelveli" },"work_history": [ {"name": "CSC",
"location": "chennai"}, {"name": "Saturam", "location": "bangalore"}, {"name": "crayon",
"location": "chennai"} ],"marital_status" :"married","disability":"No"}''')
del data["password"]
country = data["address"]["country_name"]
data.pop("disability")
data.pop("address")
data["country"] = country
i=0
for j in data["work_history"]:
if j["location"] != "chennai":
data["work_history"].pop(i)
i = i+1
print(data)