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

时间:2019-04-07 04:50:56

标签: node.js json express

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

使用axios在Express中的功能:

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] ] },

2 个答案:

答案 0 :(得分:0)

您的代码有问题,您无需stringfy之前parse

解析后,您现在可以像这样访问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()

答案 1 :(得分:-1)

要获取特定数据,您必须首先声明变量或对象。 例如,您想要emp_no,然后

var emp = body-json.emp_no

或者如果您想要薪水

var salary = body-json.salaries.salary