我想在tableviewcells上顯示我的json數據。但是,我在將json結果附加到我的配置文件homemodel時遇到問題。我得到在我的請求管理器錯誤,說不能轉換[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:)
檢索的數據樣本:
{
"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"
}
]
}
}
聽起來像對象被序列化爲一個直接的字典,而不是一個字典數組。你可以發佈你通過'NSData(contentsOfURL:)'檢索到的數據的相關片段嗎? – Palpatim
我發佈了我試圖以json格式檢索的數據 –
那麼,你的數據片段很清楚地顯示你的問題:'profile'是一個字典。你正試圖把它轉換成一個字典數組。將您的演員從'[NSDictionary]'更改爲'NSDictionary' – Palpatim