gpt4 book ai didi

node.js - 使用 JSON.stringify() 和 JSON.parse() 无法获得正确的结果

转载 作者:太空宇宙 更新时间:2023-11-04 02:45:15 25 4
gpt4 key购买 nike

尝试从 API 检索数据并将它们用作对象。问题是我在 JSON.parse() 之后没有得到正确的结果,并且我不知道如何访问数据(不知道检索特定数据的索引或参数)。

Express 中使用 axios 的函数:

var result_post = axios.post(url_post, post_data, post_config);

result_post.then(function (res){
//console.log("RESPONSE: ", res);
json_result_post = res['data'];
json_result_post = JSON.stringify(json_result_post, null, 2);
json_result_post_parse = JSON.parse(json_result_post);
console.log(json_result_post_parse);
//cconsole.log(json_result_post);
fs.writeFile("data_post.json", json_result_post, function(err){
if(err){
console.log(err);
}
});
//json_result_post_parse = JSON.parse(json_result_post);

//console.log("RESPONSE: ", res);
})
.catch(function (err){
console.log("AXIOS ERROR: ", err);
});

JSON.stringify()之后的一些数据:

{
"body-json": {
"emp_no": 80000,
"email": "80000@cloud-spartan.com",
"first_name": "Odoardo",
"last_name": "Ranft",
"birth_date": "1963-06-23",
"gender": "F",
"hire_date": "1994-07-03",
"salaries": [
{
"salary": 40000,
"from_date": "1994-07-03",
"to_date": "1995-07-03"
},
{
"salary": 41003,
"from_date": "1995-07-03",
"to_date": "1996-07-02"
},
{
"salary": 41720,
"from_date": "1996-07-02",
"to_date": "1997-07-02"
},
{
"salary": 44005,
"from_date": "1997-07-02",
"to_date": "1998-07-02"
},
{
"salary": 45659,
"from_date": "1998-07-02",
"to_date": "1999-07-02"
},
{
"salary": 46077,
"from_date": "1999-07-02",
"to_date": "2000-07-01"
},
{
"salary": 48065,
"from_date": "2000-07-01",
"to_date": "2001-07-01"
},
{
"salary": 49474,
"from_date": "2001-07-01",
"to_date": "2002-07-01"
},
{
"salary": 50059,
"from_date": "2002-07-01",
"to_date": "9999-01-01"
}
],
"departments": [
{
"dept_no": "d005",
"dept_name": "Development",
"from_date": "1994-07-03",
"to_date": "9999-01-01",
"dept_manager": [
{
"emp_no": 110511,
"first_name": "DeForest",
"last_name": "Hagimont",
"email": "110511@cloud-spartan.com",
"from_date": "1985-01-01",
"to_date": "1992-04-25"
},
{
"emp_no": 110567,
"first_name": "Leon",
"last_name": "DasSarma",
"email": "110567@cloud-spartan.com",
"from_date": "1992-04-25",
"to_date": "9999-01-01"
}
]
}
],
"title": [
{
"title": "Engineer",
"from_date": "1994-07-03",
"to_date": "2000-07-02"
},
{
"title": "Senior Engineer",
"from_date": "2000-07-02",
"to_date": "9999-01-01"
}
]
},

JSON.parse() 之后的控制台:

{ 'body-json':
{ emp_no: 80000,
email: '80000@cloud-spartan.com',
first_name: 'Odoardo',
last_name: 'Ranft',
birth_date: '1963-06-23',
gender: 'F',
hire_date: '1994-07-03',
salaries:
[ [Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object] ],
departments: [ [Object] ],
title: [ [Object], [Object] ] },

最佳答案

您的代码有问题,您不需要在解析之前stringfy

解析后,您现在可以像这样访问 emp_no:json_result_post_parse["body-json"].emp_no

下面是一个工作示例代码:

const data = `{
"body-json": {
"emp_no": 80000,
"email": "80000@cloud-spartan.com",
"first_name": "Odoardo",
"last_name": "Ranft",
"birth_date": "1963-06-23",
"gender": "F",
"hire_date": "1994-07-03",
"salaries": [
{
"salary": 40000,
"from_date": "1994-07-03",
"to_date": "1995-07-03"
},
{
"salary": 41003,
"from_date": "1995-07-03",
"to_date": "1996-07-02"
}
]
}
}`

function testjson() {
const parsedData = JSON.parse(data);
console.log(parsedData["body-json"].emp_no)
}

testjson()

关于node.js - 使用 JSON.stringify() 和 JSON.parse() 无法获得正确的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55555724/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com