PHP에서 문자열을 JSON 개체로 변환하는 방법

SQL 쿼리에서 다음과 같은 결과가 나왔습니다.

{"Coords":[
{"Accuracy":"65","Latitude":"53.277720488429026","Longitude":"-9.012038778269686","Timestamp":"Fri Jul 05 2013 11:59:34 GMT+0100 (IST)"},
{"Accuracy":"65","Latitude":"53.277720488429026","Longitude":"-9.012038778269686","Timestamp":"Fri Jul 05 2013 11:59:34 GMT+0100 (IST)"},
{"Accuracy":"65","Latitude":"53.27770755361785","Longitude":"-9.011979642121824","Timestamp":"Fri Jul 05 2013 12:02:09 GMT+0100 (IST)"},
{"Accuracy":"65","Latitude":"53.27769091555766","Longitude":"-9.012051410095722","Timestamp":"Fri Jul 05 2013 12:02:17 GMT+0100 (IST)"},
{"Accuracy":"65","Latitude":"53.27769091555766","Longitude":"-9.012051410095722","Timestamp":"Fri Jul 05 2013 12:02:17 GMT+0100 (IST)"}
] } 

현재 PHP의 문자열입니다.이미 JSON 형식으로 되어 있는 것을 알고 있습니다만, 이것을 JSON 오브젝트로 간단하게 변환할 수 있는 방법이 있습니까?

이미 “Cords”와 같은 항목/요소/개체를 추가할 수 있도록 객체가 되어야 합니다.



질문에 대한 답변



@deceze가 말한 것은 옳습니다.JSON의 형식이 잘못된 것 같습니다.이것을 시험해 주세요.

{
"Coords": [{
"Accuracy": "30",
"Latitude": "53.2778273",
"Longitude": "-9.0121648",
"Timestamp": "Fri Jun 28 2013 11:43:57 GMT+0100 (IST)"
}, {
"Accuracy": "30",
"Latitude": "53.2778273",
"Longitude": "-9.0121648",
"Timestamp": "Fri Jun 28 2013 11:43:57 GMT+0100 (IST)"
}, {
"Accuracy": "30",
"Latitude": "53.2778273",
"Longitude": "-9.0121648",
"Timestamp": "Fri Jun 28 2013 11:43:57 GMT+0100 (IST)"
}, {
"Accuracy": "30",
"Latitude": "53.2778339",
"Longitude": "-9.0121466",
"Timestamp": "Fri Jun 28 2013 11:45:54 GMT+0100 (IST)"
}, {
"Accuracy": "30",
"Latitude": "53.2778159",
"Longitude": "-9.0121201",
"Timestamp": "Fri Jun 28 2013 11:45:58 GMT+0100 (IST)"
}] } 

사용하다json_decode문자열을 개체로 변환합니다(stdClass또는 어레이: http://php.net/manual/en/function.json-decode.php

[아쉬움]

‘공식 JSON 오브젝트’가 무슨 뜻인지 잘 모르겠습니다만, PHP를 통해 json에 콘텐츠를 추가한 후 바로 JSON으로 변환하고 싶다고 생각하십니까?

다음 변수가 있다고 가정합니다.

$data = '{"Coords":[{"Accuracy":"65","Latitude":"53.277720488429026","Longitude":"-9.012038778269686","Timestamp":"Fri Jul 05 2013 11:59:34 GMT+0100 (IST)"},{"Accuracy":"65","Latitude":"53.277720488429026","Longitude":"-9.012038778269686","Timestamp":"Fri Jul 05 2013 11:59:34 GMT+0100 (IST)"},{"Accuracy":"65","Latitude":"53.27770755361785","Longitude":"-9.011979642121824","Timestamp":"Fri Jul 05 2013 12:02:09 GMT+0100 (IST)"},{"Accuracy":"65","Latitude":"53.27769091555766","Longitude":"-9.012051410095722","Timestamp":"Fri Jul 05 2013 12:02:17 GMT+0100 (IST)"},{"Accuracy":"65","Latitude":"53.27769091555766","Longitude":"-9.012051410095722","Timestamp":"Fri Jul 05 2013 12:02:17 GMT+0100 (IST)"}]}'; 

오브젝트(stdClass)로 변환해야 합니다.

$manage = json_decode($data);

단, 작업하는 것은stdClassPHP-Array보다 더 복잡합니다.그러면 이것을 사용해 보세요(두 번째 파라미터와true):

$manage = json_decode($data, true);

어레이 기능을 사용할 수 있습니다.http://php.net/manual/en/function.array.php

항목 추가:

$manage = json_decode($data, true);
echo 'Before: <br>'; print_r($manage);
$manage['Coords'][] = Array(
'Accuracy' => '90'
'Latitude' => '53.277720488429026'
'Longitude' => '-9.012038778269686'
'Timestamp' => 'Fri Jul 05 2013 11:59:34 GMT+0100 (IST)' );
echo '<br>After: <br>'; print_r($manage); 

첫 번째 항목 제거:

$manage = json_decode($data, true); echo 'Before: <br>'; print_r($manage); array_shift($manage['Coords']); echo '<br>After: <br>'; print_r($manage); 

데이터베이스 또는 파일에 json을 저장할 경우:

$data = '{"Coords":[{"Accuracy":"65","Latitude":"53.277720488429026","Longitude":"-9.012038778269686","Timestamp":"Fri Jul 05 2013 11:59:34 GMT+0100 (IST)"},{"Accuracy":"65","Latitude":"53.277720488429026","Longitude":"-9.012038778269686","Timestamp":"Fri Jul 05 2013 11:59:34 GMT+0100 (IST)"},{"Accuracy":"65","Latitude":"53.27770755361785","Longitude":"-9.011979642121824","Timestamp":"Fri Jul 05 2013 12:02:09 GMT+0100 (IST)"},{"Accuracy":"65","Latitude":"53.27769091555766","Longitude":"-9.012051410095722","Timestamp":"Fri Jul 05 2013 12:02:17 GMT+0100 (IST)"},{"Accuracy":"65","Latitude":"53.27769091555766","Longitude":"-9.012051410095722","Timestamp":"Fri Jul 05 2013 12:02:17 GMT+0100 (IST)"}]}';
$manage = json_decode($data, true);
$manage['Coords'][] = Array(
'Accuracy' => '90'
'Latitude' => '53.277720488429026'
'Longitude' => '-9.012038778269686'
'Timestamp' => 'Fri Jul 05 2013 11:59:34 GMT+0100 (IST)' );
if (($id = fopen('datafile.txt', 'wb'))) {
fwrite($id, json_encode($manage));
fclose($id); } 

제가 당신의 질문을 이해했기를 바랍니다.

행운을 빌어요.




유효한 JSON 문자열을 다시 변환하려면 이 메서드를 사용합니다.

개체를 개체로 다시 변환하려면 다음 방법을 사용합니다.

$jObj = json_decode($jsonString); 

연관 배열로 변환하려면 두 번째 파라미터를 다음과 같이 설정합니다.true:

$jArr = json_decode($jsonString, true); 

앞서 말한 문자열을 두 문자열 중 하나로 변환하려면 유효한 JSON 문자열이 있어야 합니다.이를 실현하려면 다음 작업을 수행해야 합니다.

  1. 에서Coords어레이, 2개를 삭제합니다."(큰따옴표)를 객체의 시작과 끝부터 표시합니다.
  2. 배열 내의 개체는 쉼표로 구분됩니다( ).,)의 오브젝트 사이에 콤마를 추가합니다.Coords어레이..

유효한 JSON 문자열이 생성됩니다.

다음은 유효한 JSON 문자열로 변환한 것입니다.http://pastebin.com/R16NVerw




예를 들어 이것을 사용할 수 있습니다.

$array = json_decode($string,true) 

전에 Json을 검증했습니다.http://jsonviewer.stack.hu/ 에서 검증할 수 있습니다.




두 번째 파라미터를 전달하지 않거나 false를 전달하지 않으면 json_decode()는 JSON을 stdClass 객체로 해석하기 때문에 “->” 화살표 표기법을 사용하여 객체의 속성에 액세스할 수 있습니다.

<?php
// Store JSON data in a PHP variable $json = '{"email":"john@doe.com"}';
$obj = json_decode($json, false); print $obj->email;
?> 

산출량

john@doe.com 



로 시험해 보다json_encode().

유효한 JSON을 다시 살펴보기