prosource

console.log()에 getJSON을 입력하여 json 구조를 출력합니다.

probook 2023. 2. 18. 20:30
반응형

console.log()에 getJSON을 입력하여 json 구조를 출력합니다.

json 데이터를 취득하기 위한 코드는 다음과 같습니다.

$.getJSON( "assessments", function( assessments ) {
    console.log(assessments);
        });

모든 데이터를 완벽하게 가져오고 있지만 콘솔은 다음과 같이 출력합니다.

[Object, Object, Object, Object, Object, Object, Object, Object, Object]

다음과 같이 JSON 구조로 값을 출력합니다.

[
{
    "id": 1,
    "person": {
        "personId": "person1",
        "firstName": "Pactric"
    },
    "manager": {
        "managerId": "manager1"
    },
    "state": {
        "stateId": 1,
        "description": null
    },
    "comments": null
}
]

이 데이터가 위의 JSON 구조와 정확히 일치하도록 console.log()를 표시하려면 어떻게 해야 합니까?$.getJ를 사용하고 있습니다.이 어플리케이션에는 $.ajax가 적용되지 않습니다.

로 시험해 보다.

console.log(JSON.stringify(assessments));

다음과 같이 들여쓰기로 JSON을 문자열화합니다.

$.getJSON( "assessments", function( assessments ) {
    console.log(JSON.stringify(assessments, undefined, 2))
});

JSON.stringify(value[, replacer [, space]])여기서 space는 들여쓰기입니다.MDN

언급URL : https://stackoverflow.com/questions/20500836/getjson-to-console-log-to-output-json-structure

반응형