2015-04-26 86 views
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導入是否有問題。任何幫助,將不勝感激。

非常感謝,艾倫。

回答

0

您的JSON數據是字典的數組,而不是字典。 您必須將反序列化結果轉換爲NSArray而不是NSDictionary。

改變這一行:

var dict = NSJSONSerialization.JSONObjectWithData(data!, options: nil, error: nil) as NSDictionary 

var arr = NSJSONSerialization.JSONObjectWithData(data!, options: nil, error: nil) as NSArray 

斯威夫特2更新:

還是一樣的想法,但更安全。

爲了簡單起見,我們可以使用try?與多if let

if let data = data, 
    json = try? NSJSONSerialization.JSONObjectWithData(data, options: []), 
    arr = json as? NSArray { 
    // use arr 
} 

如果你需要從NSJSONSerialization(或其他投擲方法)處理錯誤,那麼就-嘗試捕獲的方式是:

do { 
    if let data = data, 
     json = try NSJSONSerialization.JSONObjectWithData(data, options: []), 
     arr = json as? NSArray { 
     // use arr 
    } 
} catch let error as NSError { 
    print(error.localizedDescription) 
} 

此外,只是一個說明:在你的例子中,你正在使用NSData(contentsOfURL:)。可以使用它來進行試驗,但是您應該始終使用異步方法(如NSURLSession)代替實際代碼。

+1

就這麼簡單!真的很感激迅速的迴應。謝謝。 –