1

So I have the following PHP 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"}

What I need is: "Accardo was called up from Columbus on Monday, the Indians' official Twitter feed reports." and "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." as substrings. But I can't seem to figure out the best and most elegant way to do that. I tried to JSON_decode the string but I get nothing returned. Any ideas? (I am using PHP)

2
  • 2
    can you give us a snippet of the code you are using to parse it? also, notice that json_decode has a second variable that lets you choose if it's an array or an object. Commented May 17, 2012 at 6:28
  • @sfgiants2010 your json data contains many unescaped single quote, i have fixed it, check my answer. Commented May 17, 2012 at 6:36

4 Answers 4

2

That's not a string. Try like this:

$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);
Sign up to request clarification or add additional context in comments.

Comments

1

you have few unescaped string, that is causing the error. a simple formatting could have saved you the time.

$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);

Comments

0

Did you try this?

$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'];

Comments

0

json_encode with with only UTF8. are you using allthings with utf8? and you hava synstax error. if you define json variable manually it could be like this;

<?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);
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.