1
我正在尋找將一些Django json導入到iphone應用程序中。下面的Django代碼:將Django json導入iPhone應用程序
def jsonfixture(request):
data = StraightredFixture.objects.filter(fixturematchday=12)
json_data = serializers.serialize('json', data, use_natural_foreign_keys=True)
return HttpResponse(json_data, content_type='application/json')
產生以下JSON在我的瀏覽器:
[{"fields": {"awayteamscore": 2, "hometeamscore": 1, "home_team": "Stoke", "away_team": "Burnley", "fixturematchday": 12, "soccerseason": 354, "fixturedate": "2014-11-22T15:00:00", "fixturestatus": "FINISHED"}, "model": "straightred.straightredfixture", "pk": 136932}, {"fields": {"awayteamscore": 1, "hometeamscore": 2, "home_team": "ManCity", "away_team": "Swans", "fixturematchday": 12, "soccerseason": 354, "fixturedate": "2014-11-22T15:00:00", "fixturestatus": "FINISHED"}, "model": "straightred.straightredfixture", "pk": 136930}, {"fields": {"awayteamscore": 0, "hometeamscore": 0, "home_team": "Foxes", "away_team": "Sunderland", "fixturematchday": 12, "soccerseason": 354, "fixturedate": "2014-11-22T15:00:00", "fixturestatus": "FINISHED"}, "model": "straightred.straightredfixture", "pk": 137852}, {"fields": {"awayteamscore": 1, "hometeamscore": 2, "home_team": "Everton", "away_team": "West Ham", "fixturematchday": 12, "soccerseason": 354, "fixturedate": "2014-11-22T15:00:00", "fixturestatus": "FINISHED"}, "model": "straightred.straightredfixture", "pk": 136929}, {"fields": {"awayteamscore": 0, "hometeamscore": 2, "home_team": "Chelsea", "away_team": "West Bromwich", "fixturematchday": 12, "soccerseason": 354, "fixturedate": "2014-11-22T15:00:00", "fixturestatus": "FINISHED"}, "model": "straightred.straightredfixture", "pk": 136928}, {"fields": {"awayteamscore": 0, "hometeamscore": 1, "home_team": "Newcastle", "away_team": "QPR", "fixturematchday": 12, "soccerseason": 354, "fixturedate": "2014-11-22T15:00:00", "fixturestatus": "FINISHED"}, "model": "straightred.straightredfixture", "pk": 136931}, {"fields": {"awayteamscore": 2, "hometeamscore": 1, "home_team": "Arsenal", "away_team": "ManU", "fixturematchday": 12, "soccerseason": 354, "fixturedate": "2014-11-22T17:30:00", "fixturestatus": "FINISHED"}, "model": "straightred.straightredfixture", "pk": 136927}, {"fields": {"awayteamscore": 1, "hometeamscore": 3, "home_team": "Crystal", "away_team": "Liverpool", "fixturematchday": 12, "soccerseason": 354, "fixturedate": "2014-11-23T13:30:00", "fixturestatus": "FINISHED"}, "model": "straightred.straightredfixture", "pk": 136926}, {"fields": {"awayteamscore": 2, "hometeamscore": 1, "home_team": "Hull", "away_team": "Spurs", "fixturematchday": 12, "soccerseason": 354, "fixturedate": "2014-11-23T16:00:00", "fixturestatus": "FINISHED"}, "model": "straightred.straightredfixture", "pk": 136925}, {"fields": {"awayteamscore": 1, "hometeamscore": 1, "home_team": "Aston Villa", "away_team": "Southampton", "fixturematchday": 12, "soccerseason": 354, "fixturedate": "2014-11-24T20:00:00", "fixturestatus": "FINISHED"}, "model": "straightred.straightredfixture", "pk": 136924}]
然後我在Xcode以下SWIFT代碼:
let url2 = NSURL(string: "http://localhost:8000/straightred/jsonfixture")
let data = NSData(contentsOfURL: url2!)
var dict = NSJSONSerialization.JSONObjectWithData(data!, options: nil, error: nil) as NSDictionary
然而,在它的錯誤錯誤代碼「EXC_BAD_INSTRUCTION」中的第三行。
很遺憾,我不知道django json輸出或swift xcode json導入是否有問題。任何幫助,將不勝感激。
非常感謝,艾倫。
就這麼簡單!真的很感激迅速的迴應。謝謝。 –