2012-05-17 2 views
1

나는 다음과 같은 PHP 문자열이 있습니다PHP Complex String Parse, JSON 가능? 그래서

$output = {"playerId":1178,"percentChange":0.1,"averageDraftPosition":260,"percentOwned":0.1,"mostRecentNews":{"news":"Accardo was called up from Columbus on Monday, the Indians' official Twitter feed reports.","spin":"He'll replace Dan Wheeler on the active roster after carrying a 2.76 ERA over 13 appearances with the Clippers to start the season.","date":"Mon May 14"},"fullName":"Jeremy Accardo"} 

I가 필요한 것은 ". Accardo 월요일, 인디언 '공식 트위터 피드 보고서에 콜럼버스에서 불렸다" "그는 클리퍼스가 시즌을 시작하는 13 일 동안 2.76의 방어율을 기록한 뒤 활약 한 로스터에서 댄 윌러를 대신 할 것"이라고 말했다. 하위 문자열로. 그러나 나는 그것을하기에 가장 훌륭하고 우아한 방법을 찾아 낼 수 없다. 문자열을 JSON_decode하려고했지만 아무 것도 반환하지 않습니다. 어떤 아이디어? (PHP를 사용 중입니다)

+2

구문 분석에 사용하는 코드 스 니펫을 제공 할 수 있습니까? 또한 json_decode에는 배열 또는 객체인지 선택할 수있는 두 번째 변수가 있습니다. – Odinn

+0

@ sfgiants2010 json 데이터에 이스케이프 처리되지 않은 작은 따옴표가 많이 포함되어 있습니다. 해결했습니다. 내 대답을 확인하십시오. –

답변

1

오류가 발생하는 문자가 거의 없습니다. 간단한 형식 지정으로 시간을 절약 할 수있었습니다.

$output = '{ 
    "playerId":1178, 
    "percentChange":0.1, 
    "averageDraftPosition":260, 
    "percentOwned":0.1, 
    "mostRecentNews": { 
     "news":"Accardo was called up from Columbus on Monday, the Indians official Twitter feed reports", 
     "spin":"Hell replace Dan Wheeler on the active roster after carrying a 2.76 ERA over 13 appearances with the Clippers to start the season.", 
     "date":"Mon May 14" 
    }, 
    "fullName":"Jeremy Accardo" 
}'; 
$json = json_decode($output); 
0

시도해 보셨습니까?

$output = '{"playerId":1178,"percentChange":0.1,"averageDraftPosition":260,"percentOwned":0.1,"mostRecentNews":{"news":"Accardo was called up from Columbus on Monday, the Indians\' official Twitter feed reports.","spin":"He\'ll replace Dan Wheeler on the active roster after carrying a 2.76 ERA over 13 appearances with the Clippers to start the season.","date":"Mon May 14"},"fullName":"Jeremy Accardo"}'; 

$array = json_decode($output, true); 

echo $array['mostRecentNews']['news']; 
echo $array['mostRecentNews']['spin']; 
2

이것은 string이 아닙니다. 다음과 같이 시도하십시오.

$output = '{"playerId":1178,"percentChange":0.1,"averageDraftPosition":260,"percentOwned":0.1,"mostRecentNews":{"news":"Accardo was called up from Columbus on Monday, the Indians\' official Twitter feed reports.","spin":"He\'ll replace Dan Wheeler on the active roster after carrying a 2.76 ERA over 13 appearances with the Clippers to start the season.","date":"Mon May 14"},"fullName":"Jeremy Accardo"}'; 

$object = json_decode($output); 
$array = json_decode($output, true); 
$string = json_encode($array); 
0

오직 UTF8과 함께 json_encode. utf8로 allthings를 사용하고 있습니까? 그리고 당신은 synstax 오류가 있습니다. json 변수를 수동으로 정의하면 다음과 같이 될 수 있습니다.

<?php 
$output=<<<JSONSTR 
{"playerId":1178,"percentChange":0.1,"averageDraftPosition":260,"percentOwned":0.1,"mostRecentNews":{"news":"Accardo was called up from Columbus on Monday, the Indians' official Twitter feed reports.","spin":"He'll replace Dan Wheeler on the active roster after carrying a 2.76 ERA over 13 appearances with the Clippers to start the season.","date":"Mon May 14"},"fullName":"Jeremy Accardo"} 
JSONSTR; 
$variable = json_decode($output); 
var_dump($variable); 
?>