开发者

json parse error in ruby - unexpected token at

开发者 https://www.devze.com 2022-12-21 13:39 出处:网络
I get errors at a lot of places when trying to retrieve ticker symbols for US companies from http://d.yimg.com/autoc.finance.yahoo.com/autoc?callback=YAHOO.Finance.SymbolSuggest.ssCallback&query=W

I get errors at a lot of places when trying to retrieve ticker symbols for US companies from http://d.yimg.com/autoc.finance.yahoo.com/autoc?callback=YAHOO.Finance.SymbolSuggest.ssCallback&query=Wal-Mart

I have tried to:

resp = Net::HTTP.get_response(URI.parse(url))
data = resp.body
qwe = data.split("symbol")
p qwe[1]
arr1 = data.split("(")
arr2 = arr1[1].split(")")
fnl = arr2[0].gsub(/-/, '')
fnl = fnl.gsub(/\(/, '')
fnl = fnl.gsub(/\)/, '')
fnl = fnl.gsub(/\./, '')
fnl = fnl.gsub('\'', '"')
fnl = fnl.gsub(/([\{|\,}])\s*([a-zA-Z]+):/, '\1 "\2":')

But this doesnt help as i see:

/Library/Ruby/Gems/1.8/gems/json-1.2.0/lib/json/common.rb:123:in `parse': 353: unexpected token at '{"symbol":"BEEV","name": "BENCHMARK ENERGY COR开发者_StackOverflowP ' (JSON::ParserError)

Any clues as to what i might be doing wrong?


I don't know why you're doing all those replacements. It works fine once you strip the function call:

>>> pprint.pprint(json.loads(t[39:-1]))
{'ResultSet': {'Query': 'wal-mart',
               'Result': [{'exch': 'NYQ',
                           'exchDisp': 'NYSE',
                           'name': 'Wal-Mart Stores Inc.',
                           'symbol': 'WMT',
                           'type': 'S'},
                          {'exch': 'MEX',
                           'exchDisp': 'Mexico',
                           'name': 'WAL-MART-V',
                           'symbol': 'WALMEXV.MX',
                           'type': 'S'},
                          {'exch': 'TLX',
                           'name': 'WAL-MART STORES',
                           'symbol': '984101.TI',
                           'type': 'S'},
                          {'exch': 'HAM',
                           'exchDisp': 'Hamburg',
                           'name': 'WAL-MART STORES',
                           'symbol': 'WMT.HM',
                           'type': 'S'},
                          {'exch': 'STU',
                           'exchDisp': 'Stuttgart',
                           'name': 'WAL-MART-V',
                           'symbol': '4GN.SG',
                           'type': 'S'},
                          {'exch': 'FRA',
                           'exchDisp': 'Frankfurt',
                           'name': 'WAL-MART STORES',
                           'symbol': 'WMT.F',
                           'type': 'S'},
                          {'exch': 'FRA',
                           'exchDisp': 'Frankfurt',
                           'name': 'WAL-MART-V',
                           'symbol': '4GN.F',
                           'type': 'S'},
                          {'exch': 'BER',
                           'exchDisp': 'Berlin',
                           'name': 'WAL-MART STORES',
                           'symbol': 'WMT.BE',
                           'type': 'S'},
                          {'exch': 'STU',
                           'exchDisp': 'Stuttgart',
                           'name': 'WAL-MART STORES',
                           'symbol': 'WMT.SG',
                           'type': 'S'},
                          {'exch': 'BUE',
                           'exchDisp': 'Buenos Aires',
                           'name': 'WAL-MART STORES INC 2',
                           'symbol': 'DWMT2.BA',
                           'type': 'S'}]}}
0

精彩评论

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