如何使用JSONEncoder编码嵌套数组

时间:2018-05-25 11:20:38

标签: ios swift jsonencoder

我正在尝试使用JSONEncoder将四个不同的数组编码为json格式,但我无法转换数组。所以,请帮帮我。 我正在使用swift 4,xcode 9 for iOS 11。

我的阵列是:

var timeCart //dynamic data as string
var nameCart //dynamic data as string, ex. ["myNameOne", "myNameTwo", ...]
var quantityCart //dynamic data as Int, ex. [2, 3, 5, ...]
var priceCart //dynamic data as Int, ex. [82, 73, 65, ...]

我的结构是:

struct OrderDetailJSON: Encodable {
    let allOrder: [AllOrder]
    let TaxDetials: TaxDetail

    struct AllOrder: Encodable {
        let date_time: String
        let item: String
        let name: String
        let price: String
    }

    struct TaxDetail: Encodable {
        let tax_per: String
        let tax_name: String
    }
}

我试图在表格视图中对此进行编码

let orderDetailsJSON = OrderDetailJSON(allOrder: [.init(date_time: "\(quantityCart[indexPath.row])", item: "\(quantityCart[indexPath.row])", name: "\(nameCart[indexPath.row])", price: "\(priceCart[indexPath.row])")], TaxDetials: .init(tax_per: defaults.string(forKey: "STORE_TAX")!, tax_name: defaults.string(forKey: "STORE_TAX_NAME")!))
        do {
            let jsonData = try JSONEncoder().encode(orderDetailsJSON)
            let jsonString = String(data: jsonData, encoding: .utf8)
            print("order_details :" + jsonString!)
        }catch{

        }

我得到的输出是:

order_details: {
  "TaxDetials": {
    "tax_per": "5",
    "tax_name": "CGST & SGST"
  },
  "allOrder": [
    {
      "date_time": "2",
      "name": "Manchurian",
      "item": "2",
      "price": "87"
    }
  ]
}order_details: {
  "TaxDetials": {
    "tax_per": "5",
    "tax_name": "CGST & SGST"
  },
  "allOrder": [
    {
      "date_time": "3",
      "name": "Paneer Tikka",
      "item": "3",
      "price": "150"
    }
  ]
}order_details: {
  "TaxDetials": {
    "tax_per": "5",
    "tax_name": "CGST & SGST"
  },
  "allOrder": [
    {
      "date_time": "2",
      "name": "snacks",
      "item": "2",
      "price": "54"
    }
  ]
}

我期待的输出是:

order_details: {
  "TaxDetials": {
    "tax_per": "5",
    "tax_name": "CGST & SGST"
  },
  "allOrder": [
    {
      "date_time": "2",
      "name": "Manchurian",
      "item": "2",
      "price": "87"
    },
    {
      "date_time": "3",
      "name": "Paneer Tikka",
      "item": "3",
      "price": "150"
    },
    {
      "date_time": "2",
      "name": "snacks",
      "item": "2",
      "price": "54"
    }
  ]
}

1 个答案:

答案 0 :(得分:0)

你的结构声明没有任何问题(除了你应该符合Swift命名约定,这是变量名称的lowerCamelCase,taxPer而不是tax_per等等。属性名称不使用正确的复数/单数形式。)

问题是您要为每个表格视图单元格定义一个OrderDetailJSON实例,而您的TaxDetails似乎应该对表格中的所有单元格都相同,并且您实际上想要创建一个单OrderDetailJSON个实例,其allOrder数组属性包含每个表视图单元格的一个AllOrder实例。您需要将allOrder属性更改为mutable才能生效。

class YourTableViewController: UITableViewController {
    var orderDetails: OrderDetailJSON
    ...
    // You might need to use a different tableView function depending on your exact needs
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        orderDetails.allOrder[indexPath.row] = AllOrder(date_time: "\(quantityCart[indexPath.row])", item: "\(quantityCart[indexPath.row])", name: "\(nameCart[indexPath.row])", price: "\(priceCart[indexPath.row])")
    }
}

您需要确保使用正确的orderDetails初始化TaxDetails,并确保将相同数量的元素预先分配给allOrder您将拥有的多个表格视图单元格。一旦所有单元格都填满了信息,您只需要将orderDetails转换为JSON,您不需要逐个单元格地进行。