开发者

How to work with a JSON string returned by a remote URL (with Django)?

i have to build an small app in order to show some data from the Google Financial API. I know that i could study it inside out, but I don't have much time. The url http://www.google.com/finance/info?q=MSFT returns this JSON string:

// [ { "id": "358464" ,"t" : "MSFT" ,"e" : "NASDAQ" ,"l" : "24.38" ,"l_cur" : "24.38" ,"ltt":"4:00PM EDT" ,"lt" : "Oct 1, 4:00PM EDT" ,"c" : "-0.11" ,"cp" : "-0.45" ,"ccol" : "chr" ,"el": "24.39" ,"el_cur": "24.39" ,"elt" : "Oct 1, 7:58PM EDT" ,"ec" : "+0.01" ,"ecp" : "0.04" ,"eccol" : "chg" ,"div" : "0.16" ,"yld" : "2.63" } ]

I don't know how to make that string available to a view. I need to 开发者_JS百科"catch it" and show (some of) it in my template. I need something like:

def myview(...)
    URL = 'http://www.google.com/finance/info?q=MSFT'
    mystring = catchfromURL(URL)

    #work with the string

    return render_to_response('page.html', mystring)

Thanks in advance.


That little // at the beginning threw me off too. Here's what you do:

import json
jsonData = json.loads(mystring[3:])

Now, I don't know what any of the encoded data there means, but that's how you can get it as python objects.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜