WordPress获取json数据单值

时间:2014-12-03 11:33:57

标签: php json wordpress

尝试从google finances api获取单一值。我可以完美地检索数据,但是当我尝试回显单个值时,它似乎无法正常工作。有人可以帮忙吗?

我的代码是:

  $request  = wp_remote_get('http://www.google.com/finance/info?q=NASDAQ%3aGOOG', $args );                  
                $price    = wp_remote_retrieve_body( $request );

                print_r($price);

输出结果为:

    // [
    {
    "id": "304466804484872"
    ,"t" : "GOOG"
    ,"e" : "NASDAQ"
,"l" : "533.75"
,"l_fix" : "533.75"
,"l_cur" : "533.75"
,"s": "2"
,"ltt":"4:01PM EST"
,"lt" : "Dec 2, 4:01PM EST"
,"lt_dts" : "2014-12-02T16:01:56Z"
,"c" : "-0.05"
,"c_fix" : "-0.05"
,"cp" : "-0.01"
,"cp_fix" : "-0.01"
,"ccol" : "chr"
,"pcls_fix" : "533.8"
,"el": "533.00"
,"el_fix": "533.00"
,"el_cur": "533.00"
,"elt" : "Dec 2, 7:59PM EST"
,"ec" : "-0.75"
,"ec_fix" : "-0.75"
,"ecp" : "-0.14"
,"ecp_fix" : "-0.14"
,"eccol" : "chr"
,"div" : ""
,"yld" : ""
}
]

我尝试回显单个值,添加一个foreach语句,然后基于' l_fix'回显出值。和' id',并尝试拆分字符串,但它不起作用。

由于

1 个答案:

答案 0 :(得分:1)

做到:

 $request  = wp_remote_get('http://www.google.com/finance/info?q=NASDAQ%3aGOOG', $args );                  
 $data = wp_remote_retrieve_body( $request );
 $data = str_replace('//','',$data);
 $data = json_decode($data);
 $price = $data[0]; // $price = array_shift($data);
 print $price->l_fix .....

Google API(在此特定情况下)返回带有两个第一个字符的JSON('//')。