This is my JSON response in postman:
{
"result": {
"reviewcat": [
{
"review_name": "Clarity In Specification",
"id": 5
},
{
"review_name": "Communication",
"id": 6
},
{
"review_name": "Professionalism",
"id": 7
}
],
"reviewDet": {
"review_details": [
{
"review_cat_id": 5,
"review_point": "2.0"
},
{
"review_cat_id": 6,
"review_point": "4.5"
},
{
"review_cat_id": 7,
"review_point": "3",
}
],
for above response i have created model like below
public class BuyerReviewModel {
public var result : BuyserReviewResult?
}
public class BuyserReviewResult {
public var reviewcat : Array<Reviewcat>?
public var reviewDet : ReviewDet?
}
public class Reviewcat {
public var review_name : String?
public var id : Int?
}
public class ReviewDet {
public var review_details : Array<Review_details>?
}
public class Review_details {
public var review_cat_id : Int?
public var review_point : Float?// i have changed it into float
required public init?(dictionary: NSDictionary) {
if let point = dictionary["review_point"] as? NSString {
review_point = point.floatValue
}
}
}
code: with this code i am getting only first review review_point val 2.5...how do i get 2nd review_point value 4.5 how?, could anybody guide me
class PostReviewVC: UIViewController {
@IBOutlet var rateImageViews: [UIImageView]!// 5 images array
@IBOutlet var rateButtons: [UIButton]!// 5 buttons array
var rateFatched: Float = 0.0
private var byuReviewData = BuyerReviewModel(dictionary: NSDictionary()){
didSet{
titleLbl.text = byuReviewData?.result?.enquiry?.comments
idLbl.text = byuReviewData?.result?.enquiry?.ref_no
userNameLbl.text = "\(byuReviewData?.result?.reviewDet?.get_to_user?.first_name ?? "") \(byuReviewData?.result?.reviewDet?.get_to_user?.last_name ?? "")"
if let data = byuReviewData?.result?.reviewDet{
for buyReviewData in data.review_details ?? []{
rateFatched = buyReviewData.review_point ?? 0.0
if rateFatched == NSString("2.5").floatValue{
for button in rateButtons {
if button.tag >= 2 {
rateImageViews[button.tag].image = (#imageLiteral(resourceName: "staremp"))
}
else {
rateImageViews[button.tag].image = (#imageLiteral(resourceName: "star"))
}
}
}
}
}
}
}
}
my o/p look like this: here i need to show in communication 4.5 review, how with above JSON and code? plz guide me
CodePudding user response:
Yes the review_point is of type String and not Float, as suggested by @burnsi
After getting the string value you can always parse it to Float
CodePudding user response:
Since the main issue here seems to be how to get the correct data to display here is solution that focus on that but ignores any UI related stuff since I consider it off-topic.
I did some changes to the structure but nothing major, changed from class to struct, removed some optionals that didn't make sense and changed from var to let but feel free to change it back since it isn't really important to the answer.
public struct BuyserReviewResult {
public let reviewcat : [Reviewcat]
public let reviewDet : ReviewDet?
}
public struct Reviewcat {
public let review_name : String
public let id : Int
}
public struct ReviewDet {
public let review_details : [Review_details]
}
public struct Review_details {
public let review_cat_id : Int
public let review_point : Float
}
Now you can simply get each category name and the points for that category by doing
for category in result.reviewcat {
let points = result.reviewDet?.review_details
.first(where: { $0.review_cat_id == category.id })?.review_point ?? 0.0
print(category.review_name, points)
}
Note that also ignored the top level type here since it is no longer needed once you have parsed the data.

