saurabh tripathi
saurabh tripathi

Reputation: 138

how to create json object in swift 5

how can i create this json :

{"orderItems":"[{\"product_id\":19,\"quantity\":2,\"size_key\":\" 39 40 42\"},"retailer_id":20,"status":"initial"}

here is code :--

let para:NSMutableDictionary = NSMutableDictionary()
let prodArray:NSMutableArray = NSMutableArray()
para.setValue(20 , forKey: "retailer_id")
para.setValue("initial", forKey: "status")

for product in colorsArray {
  let prod: NSMutableDictionary = NSMutableDictionary()
  prod.setValue(product.product?["id"] , forKey: "product_id")
  prod.setValue("1", forKey: "quantity")
  prod.setValue(variabledata, forKey: "size_key")
  prodArray.add(prod)
}

para.setValue(20 , forKey: "retailer_id")
para.setValue("initial", forKey: "status")

Upvotes: 1

Views: 8243

Answers (4)

vadian
vadian

Reputation: 285039

You want a JSON string inside a JSON string. To accomplish that you have to encode only the array, then add the value to the dictionary and call JSONSerialization a second time.

Referring to your answer replace

para["orderItems"] =   ("\(prodArray)")

let jsonData = try! JSONSerialization.data(withJSONObject: para )
let jsonString = NSString(data: jsonData, encoding: String.Encoding.utf8.rawValue)

(By the way please never use that horrible NSString syntax in Swift)

with

let jsonData = try! JSONSerialization.data(withJSONObject: prodArray)
let arrayString = String(data: jsonData, encoding: .utf8)!
para["orderItems"] = arrayString
let resultData = try! JSONSerialization.data(withJSONObject: para)
let jsonString = String(data: resultData, encoding: .utf8)!

Upvotes: 1

saurabh tripathi
saurabh tripathi

Reputation: 138

var para : [String:Any] = [String:Any]()
var prodArray : [[String:Any]] = [[String:Any]]()

para["retailer_id"] = 20
para["status"] = "initial"

for product in colorsArray {
    var prod : [String : Any] = [String : Any]()
    if let productId = product.product?["id"] {
         prod["product_id"] = productId
    }

    prod["quantity"] =  1
    prod["size_key"] = variabledata

    prodArray.append(prod)
}

para["orderItems"] =   ("\(prodArray)")

let jsonData = try! JSONSerialization.data(withJSONObject: para )
let jsonString = NSString(data: jsonData, encoding: String.Encoding.utf8.rawValue)

output I'm getting ===

{"orderItems":[{"product_id":19,"size_key":" 394042394042","quantity":1},{"product_id":23,"quantity":1,"size_key":" 394042394042"}],"retailer_id":20,"status":"initial"}

just need value of orderItems as

"[{"product_id":19,"size_key":" 394042394042","quantity":1},{"product_id":23,"quantity":1,"size_key":" 394042394042"}]"

" " mising

Upvotes: 0

SGDev
SGDev

Reputation: 2252

Not sure about JSON but according to giving JSON and code part.

JSON should be {"orderItems" : [{"product_id" : 19 , "quantity" : 2 , "size_key" : "39 40 42"}],"retailer_id":20,"status":"initial"}

JSON creator code:

var para : [String:Any] = [String:Any]()
var prodArray : [[String:Any]] = [[String:Any]]()

para["retailer_id"] = 20
para["initial"] = "status"

for product in colorsArray {
    var prod : [String : Any] = [String : Any]()
    if let productId = product.product?["id"] {
         prod["product_id"] = productId
    }

    prod["quantity"] = "1"
    prod["size_key"] = variabledata

    prodArray.append(prod)
}

para["orderItems"] = prodArray
print(para)

Upvotes: 2

Timothy Stokarski
Timothy Stokarski

Reputation: 272

How about this version:

// MARK: - DataStructure
struct DataStructure: Codable {
    let orderItems: [OrderItem]
}

// MARK: - OrderItem
struct OrderItem: Codable {
    let productID, quantity: Int
    let sizeKey: String
    let retailerID: Int
    let status: String

    enum CodingKeys: String, CodingKey {
        case productID = "product_id"
        case quantity
        case sizeKey = "size_key"
        case retailerID = "retailer_id"
        case status
    }
}

Upvotes: 1

Related Questions