관리 메뉴

웹개발자의 기지개

[javascript] json 파싱하기 본문

javascript

[javascript] json 파싱하기

http://portfolio.wonpaper.net 2021. 3. 18. 17:12

data값이  json 데이터로 이루어져있고, javascript 형태로 불러와서 처리해야 한다.

javascript 상에서 json 을 파싱해보자.

 

data 값은

{"result":{"code":"00","message":"정상승인","advancedMessage":"정상승인","creat":"2021031155555"},"purchaseResult":{"amount":500,"trnNo":"T210311009999","authCode":"000172","balance":1305000,"ordCd":"V4040900","unitName":"계성면","grade":"초등학생","last4":"9991","settleResult":{"rate":0.018,"fee":9,"supplyAmount":8,"vat":1,"settleDay":"20210318","orgCd":"V4040000","unitName":"경상남도 창녕군"},"trackId":"test_1234","description":"Approved","udf1":"41","udf2":"1"}}

 

JSON.stringify  :  json 객체를 string 형으로 바꿀때

JSON.parse      : string 형을 object 형의 json 데이터로 파싱

 

JSON.parse(JSON.stringify(data))

 

 

간단히 소스로 정리해 보자.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!DOCTYPE html>
<html>
<body>
 
<script>
var str = '{"result":{"code":"00","message":"정상승인","advancedMessage":"정상승인","creat":"2021031155555"},"purchaseResult":{"amount":500,"trnNo":"T210311009999","authCode":"000172","balance":1305000,"ordCd":"V40409001","unitName":"계성면","grade":"초등학생","last4":"9991","settleResult":{"rate":0.018,"fee":9,"supplyAmount":8,"vat":1,"settleDay":"20210318","orgCd":"V40400001","unitName":"경상남도 창녕군"},"trackId":"test_1234","description":"Approved","udf1":"41","udf2":"1"}}';
 
//var obj = JSON.parse(JSON.stringify(str));
 
var obj = JSON.parse(str);
var val = obj.purchaseResult.amount + " / " + obj.purchaseResult.settleResult["rate"+ " / " + obj.purchaseResult.trackId;
 
alert(val);
</script>
 
</body>
</html>
cs

 

참고 : www.w3schools.com/js/tryit.asp?filename=tryjs_json_parse

Comments