简体   繁体   English

如何在Swift 5中创建json对象

[英]how to create json object in swift 5

how can i create this json : 我如何创建这个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")

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

Not sure about JSON but according to giving JSON and code part. 不确定JSON,但根据提供JSON和代码部分。

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

JSON creator code: JSON创建者代码:

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)
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 只需要orderItems的值作为

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

" " mising “”

You want a JSON string inside a JSON string. 您想要JSON字符串中的JSON字符串。 To accomplish that you have to encode only the array, then add the value to the dictionary and call JSONSerialization a second time. 要完成此操作,您只需要编码数组,然后将值添加到字典中并JSONSerialization调用JSONSerialization

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) (顺便说一句,请不要在Swift中使用那种可怕的NSString语法)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM