0

I'm really struggling to parse a JSON string with PHP. The link below contains the JSON that I'm trying to parse: http://finance.google.com/finance/info?client=ig&q=NASDAQ:MSFT,NASDAQ:GOOG,NASDAQ:AAPL

Can anyone help, I've tried everything but I'm still new to this.

Thanks.

5
  • 6
    php.net/json_decode Commented Sep 6, 2011 at 10:57
  • 1
    what have you tried? Have you looked here php.net/manual/en/function.json-decode.php Commented Sep 6, 2011 at 10:57
  • php.net/json_decode and you should probably crop off the first two or three characters of the content first. Commented Sep 6, 2011 at 10:58
  • That isn't JSON – it's invalid Javascript... Commented Sep 6, 2011 at 10:58
  • no it is obviously not invalid javascript. // it's a comment and so valid javascript :) Commented Sep 6, 2011 at 11:51

2 Answers 2

4

if there are no // in start of string use

$json_data = '[ { "id": "358464" ,"t" : "MSFT" ,"e" : "NASDAQ" ,"l" : "25.80" ,"l_cur" : "25.80" ,"s": "1" ,"ltt":"4:00PM EDT" ,"lt" : "Sep 2, 4:00PM EDT" ,"c" : "-0.41" ,"cp" : "-1.56" ,"ccol" : "chr" ,"el": "25.44" ,"el_cur": "25.44" ,"elt" : "Sep 6, 4:35AM EDT" ,"ec" : "-0.36" ,"ecp" : "-1.40" ,"eccol" : "chr" ,"div" : "0.16" ,"yld" : "2.48" } ,{ "id": "694653" ,"t" : "GOOG" ,"e" : "NASDAQ" ,"l" : "524.84" ,"l_cur" : "524.84" ,"s": "0" ,"ltt":"4:00PM EDT" ,"lt" : "Sep 2, 4:00PM EDT" ,"c" : "-7.66" ,"cp" : "-1.44" ,"ccol" : "chr" } ,{ "id": "22144" ,"t" : "AAPL" ,"e" : "NASDAQ" ,"l" : "374.05" ,"l_cur" : "374.05" ,"s": "1" ,"ltt":"4:00PM EDT" ,"lt" : "Sep 2, 4:00PM EDT" ,"c" : "-6.98" ,"cp" : "-1.83" ,"ccol" : "chr" ,"el": "371.45" ,"el_cur": "371.45" ,"elt" : "Sep 6, 6:02AM EDT" ,"ec" : "-2.60" ,"ecp" : "-0.70" ,"eccol" : "chr" ,"div" : "" ,"yld" : "" } ]';
$json_object = json_decode($json_data);

with // use

$json_data = '......';
$json_data = trim(substr($json_data,2));
$json_object = json_decode($json_data);

result

array (
  0 => 
  stdClass::__set_state(array(
     'id' => '358464',
     't' => 'MSFT',
     'e' => 'NASDAQ',
     'l' => '25.80',
     'l_cur' => '25.80',
     's' => '1',
     'ltt' => '4:00PM EDT',
     'lt' => 'Sep 2, 4:00PM EDT',
     'c' => '-0.41',
     'cp' => '-1.56',
     'ccol' => 'chr',
     'el' => '25.44',
     'el_cur' => '25.44',
     'elt' => 'Sep 6, 4:35AM EDT',
     'ec' => '-0.36',
     'ecp' => '-1.40',
     'eccol' => 'chr',
     'div' => '0.16',
     'yld' => '2.48',
  )),
  1 => 
  stdClass::__set_state(array(
     'id' => '694653',
     't' => 'GOOG',
     'e' => 'NASDAQ',
     'l' => '524.84',
     'l_cur' => '524.84',
     's' => '0',
     'ltt' => '4:00PM EDT',
     'lt' => 'Sep 2, 4:00PM EDT',
     'c' => '-7.66',
     'cp' => '-1.44',
     'ccol' => 'chr',
  )),
  2 => 
  stdClass::__set_state(array(
     'id' => '22144',
     't' => 'AAPL',
     'e' => 'NASDAQ',
     'l' => '374.05',
     'l_cur' => '374.05',
     's' => '1',
     'ltt' => '4:00PM EDT',
     'lt' => 'Sep 2, 4:00PM EDT',
     'c' => '-6.98',
     'cp' => '-1.83',
     'ccol' => 'chr',
     'el' => '371.45',
     'el_cur' => '371.45',
     'elt' => 'Sep 6, 6:02AM EDT',
     'ec' => '-2.60',
     'ecp' => '-0.70',
     'eccol' => 'chr',
     'div' => '',
     'yld' => '',
  )),
)

json_decode documentation: http://php.net/json_decode

Sign up to request clarification or add additional context in comments.

Comments

0

In your source they are commenting out the JSON. It's pretty weird that they do it, but anyway the // at the start is screwing it up.

$url = 'http://finance.google.com/finance/info?client=ig&q=NASDAQ:MSFT,NASDAQ:GOOG,NASDAQ:AAPL';
$json = preg_replace('#^\s+//#', '', file_get_contents($url) );
$decoded = json_decode( $json );
var_dump( $decoded );

3 Comments

using preg_replace, where a substr($json_data,2) would be sufficient, seems a bit overloaded…
You'd think wouldn't you? But it's not. trim() is a superfluous function.
regex - use it or loose it :)

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.