json 형식으로

json 형식으로

QA

json 형식으로

답변 4

본문

json으로 상품들의 정보를 끌어갈수 있게 만들고 있습니다.

상품의 기본적인 정보는 다 잘 됐는데, 옵션 부분을 어떻게 해야될지 모르겠습니다...ㅠ

 


header('Content-Type:application/json; charset=utf-8');
$json_array = array();
$sql =" select * from {$g5['g5_shop_item_table']} where ca_id = '$ca_id' ";
$result = sql_query($sql);
for ($i=0; $row = sql_fetch_array($result); $i++) {
-
- 생략
$json_array[$i]["it_1"] = $row["it_1"]; 
}
$json_array = json_encode($json_array, JSON_UNESCAPED_UNICODE );
$json_array = prettyPrint( $json_array );
echo prettyPrint( $json_array );

요런식으로 만들었는데...

옵션도 저런식으로 만드니깐 ...

뭔가 이상하게 나오더라고요 ㅠ

xml로는 아래와 같이 햇는데 ㅠ

 


<?
echo "<pOptions>\n";
$sql3 = " select * from {$g5['g5_shop_item_option_table']} where io_use = '1' and it_id = '{$row['it_id']}' order by io_no asc ";
$result3 = sql_query($sql3);
for($i=0; $row3=sql_fetch_array($result3); $i++) {
$opt_subject = explode(',', $row['it_option_subject']);
?>
<pOption>
<opName><?php echo $opt_subject ; ?></opName>
- 생략- 
</pOption>
<?php
}
echo "</pOptions>\n";
}
?>

 

출력하고,

파싱까지 다 잘 됐거든용 ㅠ

json이 문제라소ㅠㅠ

 

어떤식으로 json을 만들어야될까요?

 

 

이 질문에 댓글 쓰기 :

답변 4

die("{\"subject\":\"$subj\",\"content\":\"$cont\"}");

 

일반적인 json 형식입니다.

prettyPrint 무슨 함수인지는 모르겠으나..

$json = json_encode($json_array);

echo $json;

 

으로 넘겨주면 될거 같은데.. 안되시나요?

991150246_1589522012.3487.png

 

이런식으로 ㅠ

https://sir.kr/g5_plugin/3318

 

요것을 살~짝 응용했습니당

 

 

991150246_1589522562.4762.png

 

요런식으로 나와야되는뎅..ㅠ

질문에 올려주신
완성된 $json_array 데이터가 어떻게 나왔는지 보여주시겠어요?
$row["it_1"] 로 만들어진 형태라면
{"0":{"it_1":"값"},"1":{"it_1":"값"}} 이런식으로 나와야할 것 같은데..


header('Content-Type:application/json; charset=utf-8');
$json_array = array();
$sql =" select * from {$g5['g5_shop_item_table']} where ca_id = '$ca_id'  "; //
$result = sql_query($sql);
for ($i=0; $row = sql_fetch_array($result); $i++) {
   
    $json_array[$i]["code"]= specialchars_replace($row['it_id']); // 상품 고유번호
    $json_array[$i]["cate1"] = $row["ca_id"]; // 1차 카테고리
    // 생략 //
    $json_array[$i]["optionuse"] = $useoption; // 옵션 유무
    
    if($useoption =='2'){ // 옵션이 있으면
        $sql3 = " select * from {$g5['g5_shop_item_option_table']} where io_type = '0' and io_use = '1' and it_id = '{$row['it_id']}' order by io_no asc ";
        $result3 = sql_query($sql3);
        for($i=0; $row3=sql_fetch_array($result3); $i++) {
            $opt_subject = explode(',', $row['it_option_subject']);
            $json_array[$i]["opName"] = $opt_subject; 
        }
    }
}
$json_array = json_encode($json_array, JSON_UNESCAPED_UNICODE );
$json_array = prettyPrint( $json_array );
echo prettyPrint( $json_array );
}
  

 

이런식으로 했습니다...

 

$json_array[$i]["optionuse"] 와 $json_array[$i]["opName"] 는 같은 선상에 있는 배열이네요.
prettyPrint 에서 딱히 순서나 다차원 배열 이동이 없었으면 첫번째 댓글에 올려주신 이미지대로 나오는게 맞습니다.
원하시는 출력방향대로 배열부터 손보셔야할 것 같네요~

답변을 작성하시기 전에 로그인 해주세요.
QA 내용 검색
질문등록
  • 질문이 없습니다.
전체 0
© SIRSOFT
현재 페이지 제일 처음으로