2016-06-21 4 views
-4

jsonデータをtableviewcellsに表示しようとしています。しかし、私は私のプロファイルのhomemodelにjsonの結果を追加する問題を抱えています。私はそうだと言って私の要求マネージャの誤差は変換できません[NSDictionaryの] エラーを修正するには[NSDictionary]をNSDictionaryに迅速に変換できますか?

輸入財団NSDictionaryの

へ クラスcoreValueHomeModel {

//MARK: - properties 
var total: String = "total" 
var commentId: String = "commentId" 

//construct 
init(jsonData: NSDictionary){ 
    total = jsonData.objectForKey("total") as? String ?? "total" 
    commentId = jsonData.objectForKey("commentId") as? String ?? "commentId" 
} 

//construct 
init(total: String, commentId: String) { 
    self.total = total 
    self.commentId = commentId 
} 

//prints object's UserInformation 
var description: String { 
    return "total:\(total), commentId:\(commentId)" 
} 

}

輸入財団 クラスStrengthHomeModel {

//MARK: - properties 
var id: String = "id" 
var name: String = "name" 
var description: String = "description" 
var color: String = "color" 

//construct 
init(jsonData: NSDictionary){ 
    id = jsonData.objectForKey("id") as? String ?? "id" 
    name = jsonData.objectForKey("name") as? String ?? "name" 
    description = jsonData.objectForKey("description") as? String ?? "description" 
    color = jsonData.objectForKey("color") as? String ?? "color" 
} 

//construct 
init(id: String, name: String, description: String ,color: String) { 
    self.id = id 
    self.name = name 
    self.description = description 
    self.color = color 
} 

//prints object's UserInformation 
var des: String { 
    return "id: \(id), name: \(name), description: \(description), color: \(color)" 

} 

}

インポート財団 クラスProfileHomeModel {

//MARK: - properties 
var firstName: String? = "First Name" 
var lastName: String? = "Last Name" 
var location: String? = "location" 
var title: String? = "title" 
var score: String? = "score" 
var received: String? = "received" 
var given: String? = "given" 
var coreValueResults = [coreValueHomeModel]() 
var strengthResults = [StrengthHomeModel]() 

init(jsonData: NSDictionary){ 
    firstName = jsonData.objectForKey("firstName") as? String ?? "First Name" 
    lastName = jsonData.objectForKey("lastName") as? String ?? "Last Name" 
    location = jsonData.objectForKey("location") as? String ?? "location" 
    title = jsonData.objectForKey("title") as? String ?? "title" 
    score = jsonData.objectForKey("score") as? String ?? "score" 
    received = jsonData.objectForKey("received") as? String ?? "received" 
    given = jsonData.objectForKey("given") as? String ?? "given" 

    if let commentTotals = jsonData.objectForKey("commentTotals") as? [NSDictionary] { 
     for commentTotal in commentTotals { 
      let coreValue = coreValueHomeModel(jsonData: commentTotal) 
      coreValueResults.append(coreValue) 
     } 

    } 

    if let strengths = jsonData.objectForKey("strengths") as? [NSDictionary] { 
     for strength in strengths { 
      let strengthValue = StrengthHomeModel(jsonData: strength) 
      strengthResults.append(strengthValue) 
     } 

    } 

} 

init(){ 
    firstName = nil 
    lastName = nil 
    location = nil 
    title = nil 
    score = nil 
    received = nil 
    given = nil 
    coreValueResults = [] 
    strengthResults = [] 
} 

}

import Foundation 

class ProfileRequestManager { 

    func parseJson() -> ProfileHomeModel { 
     var profileValue = ProfileHomeModel() 

     let urlPath = "*********" 
     let url = NSURL(string: urlPath) 
     let data = NSData(contentsOfURL: url!) 

     do { 
      let jsonData = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments) as? NSDictionary 

       print(jsonData) 
       let profile = jsonData!.objectForKey("profile") as? NSDictionary 

      for profileInfo in profile! { 
       print(profileInfo) 
//problem here 
       profileValue.append(ProfileHomeModel(jsonData: profileInfo)) 

      } 

     } 
    catch{ 
    print("Something went wrong while parsing json data fetched from the API") 
    } 
     return profileValue 

     } 
    } 

そしてNSData(contentsOfURL:)によって検索されたデータのサンプル:あなたは配列([NSDictionary])を変換しようとしている

{ 
    "success": true, 
    "profile": { 
    "firstName": "Vignesh", 
    "lastName": "Krish", 
    "score": "126", 
    "title": "Software Developer Intern", 
    "given": "4", 
    "received": "10", 
    "commentTotals": [ 
     { 
     "total": "4", 
     "id": "8" 
     }, 
     { 
     "total": "3", 
     "id": "9" 
     }, 
     { 
     "total": "2", 
     "id": "10" 
     }, 
     { 
     "total": "1", 
     "id": "11" 
     } 
    ], 
    "strengths": [ 
     { 
     "id": "4", 
     "name": "Analytical", 
     "description": "People exceptionally talented in the Analytical theme search for reasons and causes. They have the ability to think about all the factors that might affect a situation.", 
     "color": "9c0000" 
     }, 
     { 
     "id": "17", 
     "name": "Focus", 
     "description": "People exceptionally talented in the Focus theme can take a direction, follow through, and make the corrections necessary to stay on track. They prioritize, then act.", 
     "color": "5c3a6e" 
     }, 
     { 
     "id": "8", 
     "name": "Communication", 
     "description": "People exceptionally talented in the Communication theme generally find it easy to put their thoughts into words. They are good conversationalists and presenters.", 
     "color": "da892f" 
     }, 
     { 
     "id": "29", 
     "name": "Responsibility", 
     "description": "People exceptionally talented in the Responsibility theme take psychological ownership of what they say they will do. They are committed to stable values such as honesty and loyalty.", 
     "color": "5c3a6e" 
     }, 
     { 
     "id": "30", 
     "name": "Restorative", 
     "description": "People exceptionally talented in the Restorative theme are adept at dealing with problems. They are good at figuring out what is wrong and resolving it.", 
     "color": "5c3a6e" 
     } 
    ] 
    } 
} 
+0

オブジェクトのようなサウンドは、辞書の配列ではなく、まっすぐな辞書としてシリアル化されています。 NSData(contentsOfURL:)を介して取得したデータの関連するスニペットを投稿できますか? – Palpatim

+0

私はjson形式で取得しようとしているデータを投稿しました –

+0

あなたのデータスニペットはあなたの問題をかなり明確に示しています: 'profile'は辞書です。それを辞書の配列にキャストしようとしています。あなたのキャストを '[NSDictionary]'から 'NSDictionary'に変更してください – Palpatim

答えて

0

個人にはNSDictionaryが許可されています。あなたが探している配列を取得する必要がある配列にアクセスします。

たとえば、

let myProfile = profile.firstObject 
0

それはNSDictionaryない[NSDictionary]です。

let profile = jsonData!.objectForKey("profile") as? NSDictionary 

あなたのコードには他にも問題があり、次のように意味がなくエラーが発生します。

for profile in profile { 
    print(profile) 
} 
+0

私があなたが言及したように変更を加えました。それは私にprofilehomemodelにメンバタイプが付加されていないというエラーを与えます –

+0

@VigneshKrish 'ProfileHomeModel'のコードを追加します。 – Code

+0

完了!それは投稿にあります –

0

有効なjson文字列は、配列または辞書で指定できます。 json文字列の形式がわからない場合は、その内容を操作する前にその型をチェックする必要があります。

do { 
    if let jsonData = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments) as? NSDictionary { 
     //got a dictionary, do your works with this dictionary 
    } else if let jsonData = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments) as? NSArray { 
     //got an array, do your works with this dictionary 
    } else { 
     //is not a valid json data 
    } 
} catch let _error as NSError { 
    //got error 
} 
関連する問題