1

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")
5
  • 1
    Perhaps this would help. stackoverflow.com/a/29516706/9462397 . It is a lot like typescript. Commented Nov 26, 2019 at 13:59
  • 1
    this does not contain array Commented Nov 26, 2019 at 14:02
  • 3
    That's not valid json. Commented Nov 26, 2019 at 14:05
  • 1
    That being said, I would use the Codable protocol for this. Commented Nov 26, 2019 at 14:07
  • 1
    At least never use NSMutable... collection types in Swift. Commented Nov 26, 2019 at 14:31

4 Answers 4

2

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)
Sign up to request clarification or add additional context in comments.

Comments

1

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
    }
}

2 Comments

You should also add the encoding part.
how can i use this please elaborate it .
1

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)!

Comments

0
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

1 Comment

i want orderItems values in String " " .

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.