2016-04-21 103 views
-1

iv'e一直在嘗試幾天從特定的json結構中獲取參數,但迄今爲止沒有成功。我有得到JSON數據(我需要從mHeader的mToken和數據例如)的NSDictionary從json使用swift讀取參數2

{ 
"$id" = 1; 
mHeaders =  (
      { 
     "$id" = 4; 
     mPoints = 0; 
     mRealLeagueId = 57172a6e2276fe28bcf0d91c; 
     mTeamId = 57172a762276fe28bcf0e053; 
     mTeamLogo = avatar; 
     mTeamName = "Hungry Animals"; 
    } 
); 
mToken = "5k8ziTBn0G5Gozs7qz68LCLBfLSgymOcLwRshMax1Q5pZi4bx6hbEOFgupoXrBNNGzsWosLs6KPsK6cG1kk/9o5778Y7JEKfo3CAPXS7qAg="; 
mUser =  { 
    "$id" = 2; 
    mAge = 42; 
    mCreateDate = "2016-04-20T07:06:30.507Z"; 
    mEmail = "[email protected]"; 
    mFirstName = Alesandro; 
    mGender = 1; 
    mId = 57172a762276fe28bcf0e06d; 
    mLastLogin = "2016-04-21T07:20:40.402Z"; 
    mLastName = Zohar; 
    mLogo = avatar; 
    mNick = "Big Boss 0"; 
    mRegion = Global; 
    mTeams =   (
        { 
      "$id" = 3; 
      mRealLeagueId = 57172a6e2276fe28bcf0d91c; 
      mTeamId = 57172a762276fe28bcf0e053; 
     } 
    ); 
    mTokens = 9644998; 
}; 

}

+0

仔細觀察。 'mToken'不在'mHeader'中,它在字典的根級。 – Moritz

回答

1

所以,你必須解析JSON後的字典。您需要mToken這在JSON數據的根級別,你可以簡單地通過

jsonDict["mToken"] 

獲得的價值你mHeader是字典的數組所以對於所獲得的價值,做這樣

for dict in jsonDict["mHeaders"] as! Array<NSDictionary> { 
    print(dict["mRealLeagueId"]) //prints 57172a6e2276fe28bcf0d91c 
    print(dict["mTeamId"]) //57172a762276fe28bcf0e053 
} 

希望這有助於

+0

很高興知道它有幫助。 – George