读取JSON文件,解析并导入到Parse

时间:2015-12-04 21:53:20

标签: javascript json parse-platform

我已经定义了一个获取JSON文件的Parse Cloud函数。我现在需要读取JSON数据并解析它,以便我可以使用结果填充我的一个类。问题是,我在努力知道如何在调用导入之前解析数据。在解析数据时,有人能指出我正确的方向。

My Cloud功能如下:

Parse.Cloud.define("hello1", function(request, response) {
return Parse.Cloud.httpRequest({
    url: '{feed_url_here}',
    params: {
        'LastRequest':'0',
        'SubscriberKey':'{access_key_here}',
    }
}).then(function(httpResponse) {
    response.success(httpResponse.text)
},
function (error) {
    response.error("Error: " + error.code + " " + error.message);
}); });

我的JSON样本是:

{"sports-content":{"sport-event":[{"event-metadata":{"league":"NHL Hockey","event-type":"0","league-details":"NHL","event-date-time":"12/03/2015 07:00 PM","eventNum":"2991830","status":"FINAL","off-the-board":"False"},"team":[{"team-metadata":{"alignment":"Home","nss":"2","openNum":"1","name":{"full":"New York Rangers"}},"wagering-stats":{"wagering-straight-spread":{"bookmaker-name":"CRIS","active":"true","line":"-1.5","money":"210","context":"current"},"wagering-moneyline":{"bookmaker-name":"CRIS","active":"true","line":"-135","context":"current"},"wagering-total":{"bookmaker-name":"CRIS","active":"true","line":"5.5","money":"-130","context":"current"}},"team-stats":{"score":"1"}},{"team-metadata":{"alignment":"Away","openNum":"0","nss":"1","name":{"full":"Colorado Avalanche"}},"wagering-stats":{"wagering-straight-spread":{"bookmaker-name":"CRIS","active":"true","line":"1.5","money":"-260","context":"current"},"wagering-moneyline":{"bookmaker-name":"CRIS","active":"true","line":"115","context":"current"},"wagering-total":{"bookmaker-name":"CRIS","active":"true","line":"5.5","money":"110","context":"current"}},"team-stats":{"score":"2"}}]},{"event-metadata":{"league":"NHL Hockey","event-type":"0","league-details":"NHL","event-date-time":"12/03/2015 07:00 PM","eventNum":"2991840","status":"FINAL","off-the-board":"False"},"team":[{"team-metadata":{"alignment":"Home","nss":"4","openNum":"1","name":{"full":"Carolina Hurricanes"}},"wagering-stats":{"wagering-straight-spread":{"bookmaker-name":"CRIS","active":"true","line":"-1.5","money":"200","context":"current"},"wagering-moneyline":{"bookmaker-name":"CRIS","active":"true","line":"-145","context":"current"},"wagering-total":{"bookmaker-name":"CRIS","active":"true","line":"5.0","money":"-140","context":"current"}},"team-stats":{"score":"1"}},{"team-metadata":{"alignment":"Away","openNum":"0","nss":"3","name":{"full":"New Jersey Devils"}},"wagering-stats":{"wagering-straight-spread":{"bookmaker-name":"CRIS","active":"true","line":"1.5","money":"-240","context":"current"},"wagering-moneyline":{"bookmaker-name":"CRIS","active":"true","line":"125","context":"current"},"wagering-total":{"bookmaker-name":"CRIS","active":"true","line":"5.0","money":"120","context":"current"}},"team-stats":{"score":"5"}}]},{"event-metadata":{"league":"NHL Hockey","event-type":"0","league-details":"NHL","event-date-time":"12/03/2015 07:30 PM","eventNum":"2991860","status":"FINAL","off-the-board":"False"},"team":[{"team-metadata":{"alignment":"Home","nss":"6","openNum":"1","name":{"full":"Ottawa Senators"}},"wagering-stats":{"wagering-straight-spread":{"bookmaker-name":"CRIS","active":"true","line":"1.5","money":"-260","context":"current"}

1 个答案:

答案 0 :(得分:1)

没有什么比打电话给好的'JSON.parse()

response.success(JSON.parse(httpResponse.text));

虽然您可能希望将其包装在try/catch中,因为JSON解析有时会变得毛茸茸。

try {
    response.success(JSON.parse(httpResponse.text));
} catch(e) {
    throw new Error("I don't know what that is");
}