How can I Iterate a [String:Any] - Closed

Hi there everyone,

Im trying to iterate this kind on array and initiate the Response Struct with no succes,

help would be much appriciated.


let decoded:[String:Any] = ["message-count": "1", "messages": [["message-price": "0.01880000", "status": "0", "network": "42501", "to": "123456789", "message-id": "0D0000005B86A343", "remaining-balance": "8.65950000"]]]



struct Response:Decodable {


let messageCount:Int?

let messages:[Messages]


}

struct Messages:Decodable {


let status:Int?

let messageId:String?

let to:String?

let remainingBalance:Float?

let messagePrice:Float?

let network:String?

/


init(json:[String:Any]) {

status = json["status"] as? Int ?? -1

messageId = json["message-id"] as? String ?? ""

to = json["to"] as? String ?? ""

remainingBalance = json["remaining-balance"] as? Float ?? 0.0

messagePrice = json["message-price"] as? Float ?? 0.0

network = json["network"] as? String ?? ""

}

}

Accepted Reply

CodingKeys is used to map irregular json keys to swift variables


Using Swift with Cocoa and Objective-C (Swift 4) pg 115


let json = """
{
    "message-count": 1,
    "messages": [
    {"message-price": 0.01880000, "status": 0, "network": "42501", "to": "123456789", "message-id": "0D0000005B86A343", "remaining-balance": 8.65950000}
    ]
}
""".data(using:.utf8)!
struct Response:Codable {

    let messagecount:Int
    let messages:[Message]

    enum CodingKeys: String, CodingKey {
        case messagecount = "message-count"
        case messages
    }

}
struct Message:Codable {

    let price:Float
    let status:Int
    let network:String
    let to:String
    let messageid:String
    let remainingbalance:Float

    enum CodingKeys: String, CodingKey {
        case price = "message-price"
        case status
        case network
        case to
        case messageid = "message-id"
        case remainingbalance = "remaining-balance"
    }

}
let decoder = JSONDecoder()
let response = try decoder.decode(Response.self, from: json)

print("\(response.messagecount)") // prints "1"
for message in response.messages {

    print("Price \(message.price)") // prints "Price 0.0188"

}


https://developer.apple.com/documentation/foundation/archives_and_serialization/encoding_and_decoding_custom_types


Performing the reverse


**
* The reverse */
let encode = JSONEncoder ()
let message = Message(price: 2.00, status: 1, network: "echo", to: "dexter", messageid: "9999999", remainingbalance: 3.00)
let response = Response(messagecount: 2, messages: [message])
var payload = try encode.encode(response)
let jsonEncode = String(data: payload, encoding: .utf8)!
print(jsonEncode)

/** output
{"messages":[{"status":1,"message-price":2,"to":"dexter","remaining-balance":3,"message-id":"9999999","network":"echo"}],"message-count":2}
*/

Replies

CodingKeys is used to map irregular json keys to swift variables


Using Swift with Cocoa and Objective-C (Swift 4) pg 115


let json = """
{
    "message-count": 1,
    "messages": [
    {"message-price": 0.01880000, "status": 0, "network": "42501", "to": "123456789", "message-id": "0D0000005B86A343", "remaining-balance": 8.65950000}
    ]
}
""".data(using:.utf8)!
struct Response:Codable {

    let messagecount:Int
    let messages:[Message]

    enum CodingKeys: String, CodingKey {
        case messagecount = "message-count"
        case messages
    }

}
struct Message:Codable {

    let price:Float
    let status:Int
    let network:String
    let to:String
    let messageid:String
    let remainingbalance:Float

    enum CodingKeys: String, CodingKey {
        case price = "message-price"
        case status
        case network
        case to
        case messageid = "message-id"
        case remainingbalance = "remaining-balance"
    }

}
let decoder = JSONDecoder()
let response = try decoder.decode(Response.self, from: json)

print("\(response.messagecount)") // prints "1"
for message in response.messages {

    print("Price \(message.price)") // prints "Price 0.0188"

}


https://developer.apple.com/documentation/foundation/archives_and_serialization/encoding_and_decoding_custom_types


Performing the reverse


**
* The reverse */
let encode = JSONEncoder ()
let message = Message(price: 2.00, status: 1, network: "echo", to: "dexter", messageid: "9999999", remainingbalance: 3.00)
let response = Response(messagecount: 2, messages: [message])
var payload = try encode.encode(response)
let jsonEncode = String(data: payload, encoding: .utf8)!
print(jsonEncode)

/** output
{"messages":[{"status":1,"message-price":2,"to":"dexter","remaining-balance":3,"message-id":"9999999","network":"echo"}],"message-count":2}
*/

Can you show the code where you get the json message ?


how you init the massages (struct Messages)


Note : for lisibility, class would better be named Message instead of Messages

Then:

struct Response:Decodable {
    let messageCount:Int?
    let messages:[Message]

}

Hi There Claude31

Thank you for the guidance (:



1) i get this data as a String from a curlRequest() ->String

2) and then i decode it


3) this is the part where im trying to figure out how do i the Message Struct with that data.


let str = getCurlResponse()

  if let  decoded =  try str.jsonDecode() as? [String:Any]{
  return decoded
  }

THANK YOU iTen

It works perfectly

Does your code compile ??


as iTen explained, you need to create a decoder:



let decoder = JSONDecoder()
do {
     let decoded = try decoder.decode(Response.self, from: json)
} catch {
       print("error trying to convert data to JSON"), error)
}


Have a look here for detailed explanations:

h ttps://grokswift.com/json-swift-4/

Compiles and works perfectly.

Thank you guys for the help 🙂

Great.


Keep or take time to read the detailed tutorials, you may need it in the future (I read that jsonDecoder may be tricky in some cases).


And don't forget to mark the thread as closed.

Would definitely have a look at it,

Merci, et bonne journee

Hi There iTen,

im trying to figure out how to decode the following json with the info you showed above, but i cant seem to work, i also tried to find an example in the apple Docs but without luck.

do you know maybe how it can be done ?


let json = """ 
[ 
{ 
"status_message": "Success", 
"current_carrier": { 
"network_code": "US-FIXED", 
} 
] 
""".data(using:.utf8)!