JSON雅虎财务图表数据iOS Parse

时间:2013-02-05 05:07:39

标签: iphone ios json ipad yahoo-finance

我目前正在尝试为纳斯达克,道琼斯和S& P 500创建一个股票图表。我找不到任何API(在合理的价格范围内)来创建这个,所以我一直在使用Yahoo Finance Chart API。它目前支持JSON,CSV和XML。解析XML或CSV时,性能不太理想,所以我想使用JSON响应,但我遇到了问题。 JSON响应实际上不返回有效的JSON。有没有人知道如何在iOS中解析这个或有其他建议?

以下是JSON网址: http://chartapi.finance.yahoo.com/instrument/1.1/%5EIXIC/chartdata;type=close;range=1d/json/

3 个答案:

答案 0 :(得分:1)

更新

确实,反应也是错误的。我尝试使用不同的股票代码符号的URL,看起来"字段的输出中有一个迷路previous_close标记。如果您删除了迷路"标记,则应正确验证。

"meta" :
 {
  "uri" :"/instrument/1.1/ITW/chartdata;type=close;range=1d/json/?callback=",
  "ticker" : "itw",
  "unit" : "MIN",
  "timezone" : "EST",
  "currency" : "USD",
  "gmtoffset" : -18000,
  "previous_close" : 62.9000"
 }

原件:

返回的响应是JSONP格式。

finance_charts_json_callback( {
...
} )

关闭袖口,您应该能够从开头剥离finance_charts_json_callback(,从结尾剥离),最后得到有效的JSON数据。

JSONP @ Wikipedia

答案 1 :(得分:0)

如果您使用的是iOS 5或更高版本,则可以使用NSJSONSerialization

NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

但问题是,如果JSON无效,则无法使用NSJSONSerialization解析它

我已成功将其更改为有效的JSON,如:

{
    "finance_charts_json_callback": {
        "meta": {
            "uri": "/instrument/1.1/^IXIC/chartdata;type=close;range=1d/json/",
            "ticker": "^ixic",
            "unit": "MIN",
            "timezone": "EST",
            "currency": "USD",
            "gmtoffset": -18000,
            "previous_close": "3179.1001"
        },
        "Timestamp": {
            "min": 1359988200,
            "max": 1360011600
        },
        "labels": [
            1359990000,
            1359993600,
            1359997200,
            1360000800,
            1360004400,
            1360008000,
            1360011600
        ],
        "ranges": {
            "close": {
                "min": 3131.04,
                "max": 3168.8701
            }
        },
        "series": [
            {
                "Timestamp": 1359988218,
                "close": 3160.5601
            },
            {
                "Timestamp": 1359997260,
                "close": 3139.6799
            },
            {
                "Timestamp": 1359997320,
                "close": 3139.55
            },
            {
                "Timestamp": 1359997380,
                "close": 3140.3501
            },
            {
                "Timestamp": 1359997440,
                "close": 3140.0601
            },
            {
                "Timestamp": 1359997500,
                "close": 3140.0601
            },
            {
                "Timestamp": 1359997560,
                "close": 3140.1599
            },
            {
                "Timestamp": 1359997620,
                "close": 3139.1799
            },
            {
                "Timestamp": 1359997680,
                "close": 3137.1899
            },
            {
                "Timestamp": 1359997740,
                "close": 3135.71
            },
            {
                "Timestamp": 1359997800,
                "close": 3136.54
            },
            {
                "Timestamp": 1359997860,
                "close": 3136.78
            },
            {
                "Timestamp": 1359997920,
                "close": 3137.74
            },
            {
                "Timestamp": 1359997980,
                "close": 3139.3301
            },
            {
                "Timestamp": 1359998040,
                "close": 3139.2
            },
            {
                "Timestamp": 1359998100,
                "close": 3139.0701
            },
            {
                "Timestamp": 1359998160,
                "close": 3139.04
            },
            {
                "Timestamp": 1359998220,
                "close": 3139.54
            },
            {
                "Timestamp": 1359998280,
                "close": 3139.8301
            },
            {
                "Timestamp": 1359998340,
                "close": 3139.51
            },
            {
                "Timestamp": 1359998400,
                "close": 3139.8
            },
            {
                "Timestamp": 1359998460,
                "close": 3139.97
            },
            {
                "Timestamp": 1359998520,
                "close": 3138.78
            },
            {
                "Timestamp": 1359998580,
                "close": 3138.1699
            },
            {
                "Timestamp": 1359998640,
                "close": 3138.75
            },
            {
                "Timestamp": 1359998700,
                "close": 3139.78
            },
            {
                "Timestamp": 1359998760,
                "close": 3140.4299
            },
            {
                "Timestamp": 1359998820,
                "close": 3141.01
            },
            {
                "Timestamp": 1359998880,
                "close": 3141.2
            },
            {
                "Timestamp": 1359998999,
                "close": 3141.0701
            },
            {
                "Timestamp": 1359999000,
                "close": 3141.47
            },
            {
                "Timestamp": 1359999060,
                "close": 3141.79
            },
            {
                "Timestamp": 1359999120,
                "close": 3141.8201
            },
            {
                "Timestamp": 1359999180,
                "close": 3141.8701
            },
            {
                "Timestamp": 1359999240,
                "close": 3141.72
            },
            {
                "Timestamp": 1359999300,
                "close": 3141.04
            },
            {
                "Timestamp": 1359999360,
                "close": 3142.0801
            },
            {
                "Timestamp": 1359999420,
                "close": 3141.9099
            },
            {
                "Timestamp": 1359999480,
                "close": 3142.4199
            },
            {
                "Timestamp": 1359999540,
                "close": 3142.97
            },
            {
                "Timestamp": 1359999600,
                "close": 3142.6899
            },
            {
                "Timestamp": 1359999660,
                "close": 3143.3899
            },
            {
                "Timestamp": 1359999720,
                "close": 3143.1599
            },
            {
                "Timestamp": 1359999780,
                "close": 3143.45
            },
            {
                "Timestamp": 1359999840,
                "close": 3143.26
            },
            {
                "Timestamp": 1359999900,
                "close": 3143.1699
            },
            {
                "Timestamp": 1359999960,
                "close": 3142.4099
            },
            {
                "Timestamp": 1360000020,
                "close": 3142.1799
            },
            {
                "Timestamp": 1360000080,
                "close": 3142
            },
            {
                "Timestamp": 1360000140,
                "close": 3142.28
            },
            {
                "Timestamp": 1360000200,
                "close": 3142.22
            },
            {
                "Timestamp": 1360000260,
                "close": 3141.75
            },
            {
                "Timestamp": 1360000320,
                "close": 3140.8101
            },
            {
                "Timestamp": 1360000380,
                "close": 3140.4399
            },
            {
                "Timestamp": 1360000440,
                "close": 3140.2
            },
            {
                "Timestamp": 1360000500,
                "close": 3140.3201
            },
            {
                "Timestamp": 1360000560,
                "close": 3140.1899
            },
            {
                "Timestamp": 1360000620,
                "close": 3140.4199
            },
            {
                "Timestamp": 1360000680,
                "close": 3140.21
            },
            {
                "Timestamp": 1360000740,
                "close": 3140.48
            },
            {
                "Timestamp": 1360000800,
                "close": 3140.1001
            },
            {
                "Timestamp": 1360000860,
                "close": 3140.3501
            },
            {
                "Timestamp": 1360000920,
                "close": 3140.6101
            },
            {
                "Timestamp": 1360000980,
                "close": 3140.24
            },
            {
                "Timestamp": 1360001040,
                "close": 3141.0601
            },
            {
                "Timestamp": 1360001100,
                "close": 3141.27
            },
            {
                "Timestamp": 1360001160,
                "close": 3141.5701
            },
            {
                "Timestamp": 1360001220,
                "close": 3141.6001
            },
            {
                "Timestamp": 1360001280,
                "close": 3142.8601
            },
            {
                "Timestamp": 1360001340,
                "close": 3142.8401
            },
            {
                "Timestamp": 1360001400,
                "close": 3142.6399
            },
            {
                "Timestamp": 1360001460,
                "close": 3142.8701
            },
            {
                "Timestamp": 1360001520,
                "close": 3143.1001
            },
            {
                "Timestamp": 1360001580,
                "close": 3142.8501
            },
            {
                "Timestamp": 1360001640,
                "close": 3142.8
            },
            {
                "Timestamp": 1360001700,
                "close": 3142.5901
            },
            {
                "Timestamp": 1360001760,
                "close": 3140.8501
            },
            {
                "Timestamp": 1360001820,
                "close": 3140.5
            },
            {
                "Timestamp": 1360001880,
                "close": 3140.77
            },
            {
                "Timestamp": 1360001940,
                "close": 3141.8201
            },
            {
                "Timestamp": 1360002000,
                "close": 3141.71
            },
            {
                "Timestamp": 1360002060,
                "close": 3141.9399
            },
            {
                "Timestamp": 1360002120,
                "close": 3142.49
            },
            {
                "Timestamp": 1360002180,
                "close": 3142.1399
            },
            {
                "Timestamp": 1360002240,
                "close": 3142.6499
            },
            {
                "Timestamp": 1360002300,
                "close": 3142.3999
            },
            {
                "Timestamp": 1360002360,
                "close": 3141.72
            },
            {
                "Timestamp": 1360002420,
                "close": 3141.77
            },
            {
                "Timestamp": 1360002480,
                "close": 3141.1799
            },
            {
                "Timestamp": 1360002540,
                "close": 3139.8301
            },
            {
                "Timestamp": 1360002600,
                "close": 3139.8501
            },
            {
                "Timestamp": 1360002660,
                "close": 3138.25
            },
            {
                "Timestamp": 1360002720,
                "close": 3138.74
            },
            {
                "Timestamp": 1360002780,
                "close": 3138.3799
            },
            {
                "Timestamp": 1360002840,
                "close": 3138.2
            },
            {
                "Timestamp": 1360002900,
                "close": 3138.24
            },
            {
                "Timestamp": 1360002960,
                "close": 3138.99
            },
            {
                "Timestamp": 1360003020,
                "close": 3138.8401
            },
            {
                "Timestamp": 1360003080,
                "close": 3138.5801
            },
            {
                "Timestamp": 1360003140,
                "close": 3138.3301
            },
            {
                "Timestamp": 1360003200,
                "close": 3139.0801
            },
            {
                "Timestamp": 1360003260,
                "close": 3139.25
            },
            {
                "Timestamp": 1360003320,
                "close": 3139.05
            },
            {
                "Timestamp": 1360003380,
                "close": 3139.03
            },
            {
                "Timestamp": 1360003440,
                "close": 3138.99
            },
            {
                "Timestamp": 1360003500,
                "close": 3137.99
            },
            {
                "Timestamp": 1360003560,
                "close": 3137.3601
            },
            {
                "Timestamp": 1360003679,
                "close": 3135.8799
            },
            {
                "Timestamp": 1360003680,
                "close": 3136
            },
            {
                "Timestamp": 1360003740,
                "close": 3136.49
            },
            {
                "Timestamp": 1360003800,
                "close": 3136.52
            },
            {
                "Timestamp": 1360003860,
                "close": 3135.3899
            },
            {
                "Timestamp": 1360003920,
                "close": 3135.77
            },
            {
                "Timestamp": 1360003980,
                "close": 3135.8501
            },
            {
                "Timestamp": 1360004040,
                "close": 3136.47
            },
            {
                "Timestamp": 1360004100,
                "close": 3136.4299
            },
            {
                "Timestamp": 1360004160,
                "close": 3137.46
            },
            {
                "Timestamp": 1360004220,
                "close": 3137.7
            },
            {
                "Timestamp": 1360004280,
                "close": 3137.55
            },
            {
                "Timestamp": 1360004340,
                "close": 3137.6599
            },
            {
                "Timestamp": 1360004400,
                "close": 3136.26
            },
            {
                "Timestamp": 1360004460,
                "close": 3136.6101
            },
            {
                "Timestamp": 1360004520,
                "close": 3137.02
            },
            {
                "Timestamp": 1360004580,
                "close": 3137.54
            },
            {
                "Timestamp": 1360004640,
                "close": 3138.21
            },
            {
                "Timestamp": 1360004700,
                "close": 3137.9199
            },
            {
                "Timestamp": 1360004760,
                "close": 3137.9299
            },
            {
                "Timestamp": 1360004820,
                "close": 3138.3201
            },
            {
                "Timestamp": 1360004880,
                "close": 3138.3201
            },
            {
                "Timestamp": 1360004940,
                "close": 3137.5801
            },
            {
                "Timestamp": 1360005000,
                "close": 3138.0701
            },
            {
                "Timestamp": 1360005060,
                "close": 3138.3301
            },
            {
                "Timestamp": 1360005120,
                "close": 3138.3701
            },
            {
                "Timestamp": 1360005180,
                "close": 3138.26
            },
            {
                "Timestamp": 1360005240,
                "close": 3138.23
            },
            {
                "Timestamp": 1360005300,
                "close": 3138.5
            },
            {
                "Timestamp": 1360005360,
                "close": 3139.23
            },
            {
                "Timestamp": 1360005420,
                "close": 3139.1899
            },
            {
                "Timestamp": 1360005480,
                "close": 3138.75
            },
            {
                "Timestamp": 1360005540,
                "close": 3137.6799
            },
            {
                "Timestamp": 1360005600,
                "close": 3139.03
            },
            {
                "Timestamp": 1360005660,
                "close": 3138.46
            },
            {
                "Timestamp": 1360005720,
                "close": 3138.05
            },
            {
                "Timestamp": 1360005780,
                "close": 3137.96
            },
            {
                "Timestamp": 1360005840,
                "close": 3138.1101
            },
            {
                "Timestamp": 1360005900,
                "close": 3136.8701
            },
            {
                "Timestamp": 1360005960,
                "close": 3136.71
            },
            {
                "Timestamp": 1360006020,
                "close": 3136.73
            },
            {
                "Timestamp": 1360006080,
                "close": 3138.22
            },
            {
                "Timestamp": 1360006140,
                "close": 3139.8501
            },
            {
                "Timestamp": 1360006200,
                "close": 3140.49
            },
            {
                "Timestamp": 1360006260,
                "close": 3140.8201
            },
            {
                "Timestamp": 1360006320,
                "close": 3139.8401
            },
            {
                "Timestamp": 1360006380,
                "close": 3140.28
            },
            {
                "Timestamp": 1360006440,
                "close": 3139.73
            },
            {
                "Timestamp": 1360006500,
                "close": 3139.3
            },
            {
                "Timestamp": 1360006560,
                "close": 3140.1599
            },
            {
                "Timestamp": 1360006620,
                "close": 3139.6299
            },
            {
                "Timestamp": 1360006680,
                "close": 3140.0901
            },
            {
                "Timestamp": 1360006740,
                "close": 3140.24
            },
            {
                "Timestamp": 1360006800,
                "close": 3140.3101
            },
            {
                "Timestamp": 1360006860,
                "close": 3140.8401
            },
            {
                "Timestamp": 1360006920,
                "close": 3140.95
            },
            {
                "Timestamp": 1360006980,
                "close": 3140.8799
            },
            {
                "Timestamp": 1360007040,
                "close": 3142.1101
            },
            {
                "Timestamp": 1360007100,
                "close": 3142.1399
            },
            {
                "Timestamp": 1360007160,
                "close": 3141.8301
            },
            {
                "Timestamp": 1360007220,
                "close": 3141.0701
            },
            {
                "Timestamp": 1360007280,
                "close": 3140.99
            },
            {
                "Timestamp": 1360007340,
                "close": 3140.79
            },
            {
                "Timestamp": 1360007400,
                "close": 3141.1499
            },
            {
                "Timestamp": 1360007460,
                "close": 3140.76
            },
            {
                "Timestamp": 1360007520,
                "close": 3139.6499
            },
            {
                "Timestamp": 1360007580,
                "close": 3140.0701
            },
            {
                "Timestamp": 1360007640,
                "close": 3140.51
            },
            {
                "Timestamp": 1360007700,
                "close": 3140.98
            },
            {
                "Timestamp": 1360007760,
                "close": 3141.3601
            },
            {
                "Timestamp": 1360007820,
                "close": 3142.46
            },
            {
                "Timestamp": 1360007880,
                "close": 3143.3899
            },
            {
                "Timestamp": 1360007940,
                "close": 3143.24
            },
            {
                "Timestamp": 1360008000,
                "close": 3142.01
            },
            {
                "Timestamp": 1360008060,
                "close": 3142.02
            },
            {
                "Timestamp": 1360008120,
                "close": 3141.0701
            },
            {
                "Timestamp": 1360008180,
                "close": 3141.25
            },
            {
                "Timestamp": 1360008240,
                "close": 3141.3999
            },
            {
                "Timestamp": 1360008300,
                "close": 3141.3799
            },
            {
                "Timestamp": 1360008360,
                "close": 3141.74
            },
            {
                "Timestamp": 1360008420,
                "close": 3141.96
            },
            {
                "Timestamp": 1360008480,
                "close": 3141.3201
            },
            {
                "Timestamp": 1360008540,
                "close": 3140.79
            },
            {
                "Timestamp": 1360008600,
                "close": 3140.8201
            },
            {
                "Timestamp": 1360008660,
                "close": 3140.73
            },
            {
                "Timestamp": 1360008720,
                "close": 3139.6299
            },
            {
                "Timestamp": 1360008780,
                "close": 3139.54
            },
            {
                "Timestamp": 1360008840,
                "close": 3140.29
            },
            {
                "Timestamp": 1360008900,
                "close": 3140.3899
            },
            {
                "Timestamp": 1360008960,
                "close": 3140
            },
            {
                "Timestamp": 1360009020,
                "close": 3140.04
            },
            {
                "Timestamp": 1360009080,
                "close": 3139.8101
            },
            {
                "Timestamp": 1360009140,
                "close": 3140.1799
            },
            {
                "Timestamp": 1360009200,
                "close": 3139.9099
            },
            {
                "Timestamp": 1360009260,
                "close": 3139.8899
            },
            {
                "Timestamp": 1360009320,
                "close": 3140.22
            },
            {
                "Timestamp": 1360009380,
                "close": 3140.1899
            },
            {
                "Timestamp": 1360009440,
                "close": 3138.8501
            },
            {
                "Timestamp": 1360009500,
                "close": 3140.3301
            },
            {
                "Timestamp": 1360009560,
                "close": 3141.0901
            },
            {
                "Timestamp": 1360009679,
                "close": 3141.97
            },
            {
                "Timestamp": 1360009680,
                "close": 3141.7
            },
            {
                "Timestamp": 1360009740,
                "close": 3141.73
            },
            {
                "Timestamp": 1360009800,
                "close": 3140.1399
            },
            {
                "Timestamp": 1360009860,
                "close": 3139.6499
            },
            {
                "Timestamp": 1360009920,
                "close": 3138.95
            },
            {
                "Timestamp": 1360009980,
                "close": 3138.49
            },
            {
                "Timestamp": 1360010040,
                "close": 3138.1001
            },
            {
                "Timestamp": 1360011600,
                "close": 3131.5
            }
        ]
    }
}

您需要使用 NSString 类提供的各种功能替换无效字符并使用jsonlint tool检查它是否有效。

答案 2 :(得分:0)

你需要按照以下方式执行javascript eval:

window.finance_charts_json_callback(data) { return data; }
let json_resp = eval(resp);
在ios中,您可以创建javascript解释器并对其进行评估。