LANGUAGE/PHP
[PHP] array to json, json to array
보겸삼촌
2020. 2. 27. 15:02
받는 JSON형식 예시
$sample = array(
"intent" => array(
"id" => "testid",
"name" => "",
"extra" => array(
"code" => "",
"message" => ""
)
),
"userRequest" => array(
"timezone" => "",
"block" => array(
"id" => "",
"name" => ""
),
"utterance" => "",
"lang" => "",
"user" => array(
"id" => "",
"type" => "",
"properties" => array(
"plusfriendUserKey" => "",
"appUserId" => ""
)
)
),
"bot" => array(
"id" => "1234",
"name" => "5678"
),
"action" => array(
"id" => "",
"name" => "",
"params" => "",
"detailParams" => "",
"clientExtra" => ""
),
"context" => "test"
);
실행코드
//array to json
$output = json_encode($sample);
// echo $output;
//json to array
$json_string = $output;
$data = json_decode($json_string, true);
echo $data['context']."<br />";
echo $data['bot']['id']."<br />";
//error
if(json_last_error() > 0){
echo json_last_error_msg().PHP_EOL;
}
결과