2014-04-13 8 views
-1

Я пытаюсь преобразовать этот ответ в PHP массив:Как преобразовать ответ JSON в PHP массив

string(734) " {"definitions":[{"text":"Informal One who is proficient at using or programming a computer; a computer buff.","attribution":"from The American Heritage\u00ae Dictionary of the English Language, 4th Edition"},{"text":"Informal One who uses programming skills to gain illegal access to a computer network or file.","attribution":"from The American Heritage\u00ae Dictionary of the English Language, 4th Edition"},{"text":"Informal One who enthusiastically pursues a game or sport: a weekend tennis hacker. ","attribution":"from The American Heritage\u00ae Dictionary of the English Language, 4th Edition"},{"text":"See hackie.","attribution":"from The American Heritage\u00ae Dictionary of the English Language, 4th Edition"}]}"

Я попробовал все, от

$string = $result; $pattern = '\w*\(\d+\)\s'; $replacement = ''; preg_replace($pattern,$replacement,$string)

к

$string = $result; $pattern = '\w*\(\d+\)\s'; $replacement = ''; $def_array = str_replace($pattern,$replacement,$string);

, но когда я делаю это, они даже не меняют исходный результат.

Я хочу, чтобы достичь чего-то вроде этого: $def_array = {"definitions":[{"text":"Informal One who is proficient at using or programming a computer; a computer buff.","attribution":"from The American Heritage\u00ae Dictionary of the English Language, 4th Edition"},{"text":"Informal One who uses programming skills to gain illegal access to a computer network or file.","attribution":"from The American Heritage\u00ae Dictionary of the English Language, 4th Edition"},{"text":"Informal One who enthusiastically pursues a game or sport: a weekend tennis hacker. ","attribution":"from The American Heritage\u00ae Dictionary of the English Language, 4th Edition"},{"text":"See hackie.","attribution":"from The American Heritage\u00ae Dictionary of the English Language, 4th Edition"}]}

Я хочу, чтобы пройти через каждое определение, как это:

for($i = 0; $i < sizeof($def_array[0]["definitions"]); $i++) { echo $def_array[0]["definitions"][$i]["text"]; echo "\n"; }

Я не использую json_decode, потому что, когда я могу получить ошибка Fatal error: Cannot use object of type stdClass as array on the line for ($ i = 0; $ i < sizeof ($ def_array [0] ["определения"]); $ i ++) `.

+1

Не забудьте выбрать ответ, если вы зафиксировали проблему. – NMC

ответ

4

Почему не только функция json_decode().

$array = json_decode($input); 

Найти больше информации здесь: http://www.php.net/manual/en/function.json-decode.php

+0

dat simplicity ... –

+0

потому что, когда я делаю 'for ($ i = 0; $ i hexicle

+0

@raindrop Использовать 'json_decode ($ result, true);'. Вместо этого вы можете использовать '$ def_array [0] -> определения [$ i] -> text'. –

0
Parse error on line 1: 
"{ "definitions" 
^ 
Expecting '{', '[' 

Формат не является действительным, оно должно начинаться с {или [

Смежные вопросы