0

I am trying to parse out a int value in the variable but its returning nil or 0 The value I want is from the customer_group_id. I am using swift. I have parsed out other information from the json file, but it keeps messing up at this stage.

  print("My Loyalty rank: " , newJson["loyalty_rank"][0]["customer_group_id"].string)
  myRank = newJson["loyalty_rank"][0]["customer_group_id"].intValue

Here is the json file

{ "id":298,
  "name":"桃子 桜",
  "total_points_earned":164, 
  "points_available_to_spend":164,
  "loyalty_rank":{ 
       "customer_group_id":9,
       "name":"Baby Bee",
       "ratio":1
   },
  "order_history":[ 
  { }
  ],
 "barcode_id":"C-00000298"
 }

1 Answer 1

2

loyalty_rank is [String:AnyObject] means its Dictionary not an array ...

try to print it like

     // if its string
     print("My Loyalty rank: " , newJson["loyalty_rank"]["customer_group_id"].string)

     // if its Int
     print("My Loyalty rank: " , newJson["loyalty_rank"]["customer_group_id"].intValue)

Better to use if let or guard like

    if let customerId = newJson["loyalty_rank"]["customer_group_id"].intValue{
        // customerId is int value
   }
   else{
       // not an int
   }
Sign up to request clarification or add additional context in comments.

2 Comments

its in a nested try catch block. But thats a really good point to use a if statement if its not in a try
Sorry I couldn't accept it as the answer for 10 min for some reason. Thanks again I appreciate it. I also got lost programming too my bad.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.