1

Could some one please help me out on this I have the following json string

string(1223) "YAHOO.Finance.SymbolSuggest.ssCallback({"ResultSet":{"Query":"google","Result":[{"symbol":"GOOG","name": "Google Inc.","exch": "NMS","type": "S","exchDisp":"NASDAQ","typeDisp":"Equity"},{"symbol":"GOOG.MX","name": "GOOGLE-A","exch": "MEX","type": "S","exchDisp":"Mexico","typeDisp":"Equity"},{"symbol":"GGQ1.F","name": "GOOGLE-A","exch": "FRA","type": "S","exchDisp":"Frankfurt","typeDisp":"Equity"}]}})" 

But I cannot seem to get anywhere with it. Basically I want to just loop out the the results which are

[{"symbol":"GOOG","name": "Google Inc.","exch": "NMS","type": "S","exchDisp":"NASDAQ","typeDisp":"Equity"},{"symbol":"GOOG.MX","name": "GOOGLE-A","exch": "MEX","type": "S","exchDisp":"Mexico","typeDisp":"Equity"},{"symbol":"GGQ1.F","name": "GOOGLE-A","exch": "FRA","type": "S","exchDisp":"Frankfurt","typeDisp":"Equity"}]

Sorry my question is how can I loop or even print the first result for example

{"symbol":"GOOG","name": "Google Inc.","exch": "NMS","type": "S","exchDisp":"NASDAQ","typeDisp":"Equity"}
1
  • Oops I misunderstand question wrong in the beginning also because of your formatting a little bit you need to specify how you query(API) for that data(JSON-P). You should not provide callback to it. Commented Jan 26, 2011 at 12:14

2 Answers 2

3

Your string is not JSON, it is JSON-in-Script. Notice the fragment that says:

YAHOO.Finance.SymbolSuggest.ssCallback(...)

When a browser receives the above mentioned script (actually a javascript code) it will call the YAHOO.Finance.SymbolSuggest.ssCallback function, passing the JSON data as the argument.

You did not mention if you want to access the JASON data on the server side or client? It its server side (PHP) then you can use regular expressions or string replacement functions to extract the portion you like. The you can use json_decode() function to convert the resulting string into an associative array.

Edit ----

A quick and dirty hack for converting JSONP to JSON:

<?php
    $text = 'YAHOO.Finance.SymbolSuggest.ssCallback({"ResultSet":{"Query":"google","Result":[{"symbol":"GOOG","name": "Google Inc.","exch": "NMS","type": "S","exchDisp":"NASDAQ","typeDisp":"Equity"},{"symbol":"GOOG.MX","name": "GOOGLE-A","exch": "MEX","type": "S","exchDisp":"Mexico","typeDisp":"Equity"},{"symbol":"GGQ1.F","name": "GOOGLE-A","exch": "FRA","type": "S","exchDisp":"Frankfurt","typeDisp":"Equity"}]}})';
    # //CONVERT JSONP to JSON\\
    $text = preg_replace('/.+?({.+}).+/', '$1', $text); 
    # \\CONVERT JSONP to JSON//
    $data = json_decode($text);
    var_dump($data);
    var_dump($data->ResultSet->Result[0]);
    var_dump($data->ResultSet->Result[0]->symbol);
    var_dump($data->ResultSet->Result[0]->name);
    # etc etc
?>
Sign up to request clarification or add additional context in comments.

2 Comments

Which would be the best way to do it client or server side ?
I am not sure where you're grabbing that string from. You mentioned php in the tags and a print_r of the result so I posted a PHP solution. If you're using Yahoo JavaScript libraries, you should be able to do it on the client side.
1

Your result is not just a JSON string, it's a JSON string prepended by a call to a JSON function. This is quite certainly a JSONP call. You must write the YAHOO.Finance.SymbolSuggest.ssCallback(data) javascript function and get the Json there. Check the JSONP query, you should be able to alter the name of this backreference function if you want another name, it's usually on of the parameter in the GET query.

Now you are maybe calling it directly from PHP and you are not in js envirronment. so you must write something in your PHP code to remove the YAHOO.Finance.SymbolSuggest.ssCallback( part and the ) at the end before parsing it as JSON data..

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.