0

I have the json

{
  "message": null,
  "data": {
    "Commodity Department": {
      "total": 2,
      "completed": 1,
      "completedWithDue": 0,
      "completedWithOutDue": 1,
      "inProgress": 1,
      "inProgressWithDue": 0,
      "inProgressWithOutDue": 1,
      "statusCounter": null
    }
}

I need to convert the each department json object to array. Currently each category value ("total": 0, "completed": 0, "completedWithDue": 0, "completedWithOutDue": 0, "inProgress": 0, "inProgressWithDue": 0, "inProgressWithOutDue": 0,) will be in object format. I need to convert in array and load to collectionview based on category. As of now I am trying to decode my json in the below code

public struct Dashboard: Decodable {
    public let data : [String:Departments]
}

public struct Departments: Decodable {
    public let total, completed, completedWithDue, completedWithOutDue: Int
    public let inProgress, inProgressWithDue, inProgressWithOutDue: Int
}

let dashboard = try? JSONDecoder().decode(Dashboard.self, from: response.data!)

print(dashboard!.data.keys)
10
  • 1
    What is the question? Commented Dec 20, 2019 at 12:34
  • @vadian currently each category value ("total": 0, "completed": 0, "completedWithDue": 0, "completedWithOutDue": 0, "inProgress": 0, "inProgressWithDue": 0, "inProgressWithOutDue": 0,) will be in object format. i need to convert array load to collectionview based on category Commented Dec 20, 2019 at 12:36
  • For readability, please add code to your question instead of in the comments. Also, what is the output of print(dashboard!.data.keys) ? Commented Dec 20, 2019 at 12:41
  • Does this answer your question? How to parse Array of JSON to array in Swift Commented Dec 20, 2019 at 12:43
  • @koen I get the deparment names All Departments,Portfolio Mangement Service..... Commented Dec 20, 2019 at 12:44

2 Answers 2

0

You can create an array of the values by adding a computed property to your struct

public struct Departments: Decodable {
    public let total, completed, completedWithDue, completedWithOutDue: Int
    public let inProgress, inProgressWithDue, inProgressWithOutDue: Int

    var categoryValues: [Int] {get {
        return [total, completed, completedWithDue, completedWithOutDue,
         inProgress, inProgressWithDue, inProgressWithOutDue]
        }
    }
}

or create the array on the fly using map

dashboard?.data.mapValues{[$0.total, $0.completed, $0.completedWithDue, ...]}
Sign up to request clarification or add additional context in comments.

4 Comments

I got a error Missing return in a function expected to return '[Int]'
Sorry, it's a new feature in Swift 5.
How to access the keys
There are no keys in an array but the values are always in the same order. If you want keys you need a dictionary, the solution is basically the same
0

You can decode json without knowing keys like this:

func printJSON(json: [String:Any]) {
let jsonKeys = json.keys //Gets the list of keys on the outer-most layer of the JSON
for i in 0..<jsonKeys.count {
    let level1 = json[jsonKeys.index(jsonKeys.startIndex, offsetBy: i)] //retrieves the object with the specific keys
    if let level2 = json[level1.key] as? [String:Any]{ //if the key is another object
        printJSON(json: level2) //send it as a new json object to the function again
    } else if let level2 = json[level1.key] as? [[String:Any]] { //if the key is an array of objects
        for i in 0..<level2.count { //loop through the array
            printJSON(json: level2[i]) //send each array element to the function
        }
    } else if let value = json[level1.key] as? String { //if value of String/Integer/Bool type
        print(value) //then only print values of the specified type (String-type in this case)
    }
}

}

See full article Here

Comments

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.