json 형식 php로 다시한번만 봐주세요..
본문
스크립트로 확인하는건 그누위즈님이 답변 주셔서 해결했습니다.
$test="{\"result\":{\"resultCd\":\"0000\",\"resultMsg\":\"성공\",\"advanceMsg\":\"정상승인\",\"create\":\"20240410001244\"},\"pay\":{\"authCd\":\"82077601\",\"card\":{\"cardId\":\"card_0dad-000a9-35d-e7a5e\",\"installment\":0,\"bin\":\"518831\",\"last4\":\"3218\",\"issuer\":\"삼성\",\"cardType\":\"신용\",\"acquirer\":\"삼성\",\"issuerCode\":\"04\",\"acquirerCode\":\"04\"},\"webhookUrl\":\"\",\"products\":[{\"prodId\":\"\",\"name\":\"상품명패키지(박스+미포함-3)\",\"qty\":1,\"price\":1000,\"desc\":\"description\"}],\"trxId\":\"T240331783861\",\"trxType\":\"3DTR\",\"tmnId\":\"TMN043159\",\"trackId\":\"20240411161206-1394\",\"amount\":1000,\"udf1\":\"1394\",\"udf2\":\"\"}}";
$data = json_decode($test, true);
echo $resultMsg = $data['result']['resultMsg'];
echo $trxId= $data['result']['trxId'];
값을 가져올수가 없는데
$test 변수에 저런형태로 있을경우. 어떻게 원하는 값을추출을 해야하나요?
답변 4
<?php
$test="{\"result\":{\"resultCd\":\"0000\",\"resultMsg\":\"성공\",\"advanceMsg\":\"정상승인\",\"create\":\"20240410001244\"},\"pay\":{\"authCd\":\"82077601\",\"card\":{\"cardId\":\"card_0dad-000a9-35d-e7a5e\",\"installment\":0,\"bin\":\"518831\",\"last4\":\"3218\",\"issuer\":\"삼성\",\"cardType\":\"신용\",\"acquirer\":\"삼성\",\"issuerCode\":\"04\",\"acquirerCode\":\"04\"},\"webhookUrl\":\"\",\"products\":[{\"prodId\":\"\",\"name\":\"상품명패키지(박스+미포함-3)\",\"qty\":1,\"price\":1000,\"desc\":\"description\"}],\"trxId\":\"T240331783861\",\"trxType\":\"3DTR\",\"tmnId\":\"TMN043159\",\"trackId\":\"20240411161206-1394\",\"amount\":1000,\"udf1\":\"1394\",\"udf2\":\"\"}}";
$data = json_decode($test, true);
echo $resultMsg = $data['result']['resultMsg'];
//echo $trxId= $data['result']['trxId'];
echo $trxId= $data['pay']['trxId'];
?>
var_dump($data); 이렇게 해서 배열이 정상적으로 보여지고 있는지 부터 확인해 보세요
$test='{"result":{"resultCd":"0000","resultMsg":"성공","advanceMsg":"정상승인","create":"20240410001244"},"pay":{"authCd":"82077601","card":{"cardId":"card_0dad-000a9-35d-e7a5e","installment":0,"bin":"518831","last4":"3218","issuer":"삼성","cardType":"신용","acquirer":"삼성","issuerCode":"04","acquirerCode":"04"},"webhookUrl":"","products":[{"prodId":"","name":"상품명패키지(박스+미포함-3)","qty":1,"price":1000,"desc":"description"}],"trxId":"T240331783861","trxType":"3DTR","tmnId":"TMN043159","trackId":"20240411161206-1394","amount":1000,"udf1":"1394","udf2":""}}';
$data = json_decode($test, true);
echo $resultMsg = $data['result']['resultMsg'];
echo $trxId= $data['result']['trxId'];
\를 제거했는데도 문제가 있네요..
json으로 받은변수로 알고있었는데..
<?php
$test='{"result":{"resultCd":"0000","resultMsg":"성공","advanceMsg":"정상승인","create":"20240410001244"},"pay":{"authCd":"82077601","card":{"cardId":"card_0dad-000a9-35d-e7a5e","installment":0,"bin":"518831","last4":"3218","issuer":"삼성","cardType":"신용","acquirer":"삼성","issuerCode":"04","acquirerCode":"04"},"webhookUrl":"","products":[{"prodId":"","name":"상품명패키지(박스+미포함-3)","qty":1,"price":1000,"desc":"description"}],"trxId":"T240331783861","trxType":"3DTR","tmnId":"TMN043159","trackId":"20240411161206-1394","amount":1000,"udf1":"1394","udf2":""}}';
// JSON문자열 → 배열
$arr = json_decode($test, true);
echo $arr['pay']['trxId']; // T240331783861
// JSON문자열 → 객체
$obj = json_decode($test);
echo $obj->pay->trxId; // T240331783861