How to get JSON data from an API
Asked Answered
P

2

5

i have used yahoo's symbol lookup

http://d.yimg.com/autoc.finance.yahoo.com/autoc?query=yahoo&callback=YAHOO.Finance.SymbolSuggest.ssCallback

which returns data in JSON format. like following

YAHOO.Finance.SymbolSuggest.ssCallback(
{
    "ResultSet": {
        "Query": "ya",
        "Result": [
            {
                "symbol": "YHOO",
                "name": "Yahoo! Inc.",
                "exch": "NMS",
                "type": "S",
                "exchDisp": "NASDAQ"
            },
            {
                "symbol": "AUY",
                "name": "Yamana Gold, Inc.",
                "exch": "NYQ",
                "type": "S",
                "exchDisp": "NYSE"
            },
            {
                "symbol": "YZC",
                "name": "Yanzhou Coal Mining Co. Ltd.",
                "exch": "NYQ",
                "type": "S",
                "exchDisp": "NYSE"
            },
            {
                "symbol": "YRI.TO",
                "name": "YAMANA GOLD INC COM NPV",
                "exch": "TOR",
                "type": "S",
                "exchDisp": "Toronto"
            },
            {
                "symbol": "8046.TW",
                "name": "NAN YA PRINTED CIR TWD10",
                "exch": "TAI",
                "type": "S",
                "exchDisp": "Taiwan"
            },
            {
                "symbol": "600319.SS",
                "name": "WEIFANG YAXING CHE 'A'CNY1",
                "exch": "SHH",
                "type": "S",
                "exchDisp": "Shanghai"
            },
            {
                "symbol": "1991.HK",
                "name": "TA YANG GROUP",
                "exch": "HKG",
                "type": "S",
                "exchDisp": "Hong Kong"
            },
            {
                "symbol": "1303.TW",
                "name": "NAN YA PLASTIC TWD10",
                "exch": "TAI",
                "type": "S",
                "exchDisp": "Taiwan"
            },
            {
                "symbol": "0294.HK",
                "name": "YANGTZEKIANG",
                "exch": "HKG",
                "type": "S",
                "exchDisp": "Hong Kong"
            },
            {
                "symbol": "YAVY",
                "name": "Yadkin Valley Financial Corp.",
                "exch": "NMS",
                "type": "S",
                "exchDisp": "NASDAQ"
            }
        ]
    }
}
)

i want to get Result 1st array data

i am trying by using the below but its not working for me

$file = "http://d.yimg.com/autoc.finance.yahoo.com/autoc?query=yahoo&callback=YAHOO.Finance.SymbolSuggest.ssCallback";
$data = file_get_contents($file);
$result = json_decode($data);

i want to get the result 1st array symbol

i used

$result ['YAHOO.Finance.SymbolSuggest.ssCallback']['ResultSet']['result']['symbol']

its not working me, please help me, how i can get symbol from the above API

Thanks Sanjib

Predella answered 10/7, 2015 at 6:10 Comment(1)
Your Json is invalide jsonlint.comSeeto
B
7

Try this

<?php
$file = "http://d.yimg.com/autoc.finance.yahoo.com/autoc?query=yahoo&callback=YAHOO.Finance.SymbolSuggest.ssCallback";
$data = file_get_contents($file);
$data = mb_substr($data, strpos($data, '{'));
$data = mb_substr($data, 0, -1);
$result = json_decode($data, true);
print_r($result['ResultSet']['Result'][0]);
Bevvy answered 10/7, 2015 at 6:21 Comment(3)
Shouldn't you add some comments so everyone knows what you are doing and how you are validating the Json?Seeto
Code is self explanatory. By default data is returned in format to be used by Javascript. I'm just cutting of parts which are not needed (like callback function). What remains is JSON. From there just decode it as array and you are done.Bevvy
yes, i understand your code, your thinking was great :)Predella
V
0

Just remove the callback parameter from the request url. Then you will be given a valid JSON-object. E.g. in your case it would be:
http://d.yimg.com/autoc.finance.yahoo.com/autoc?query=yahoo
Instead of:
http://d.yimg.com/autoc.finance.yahoo.com/autoc?query=yahoo&callback=YAHOO.Finance.SymbolSuggest.ssCallback

Vicenta answered 26/1, 2016 at 9:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.