ios - 以给定格式 Swift 创建 JSON

标签 ios json swift nsdictionary nsmutabledictionary

我想创建以下格式的 JSON。

   {
  "userID": "1234",
  "cart_items": {
    "1": {
      "item_Seq_no": 1,
      "catalog_id": 234,
      "qty": 1,
      "amount": 100
    },
    "2": {
      "item_Seq_no": 2,
      "catalog_id": 45,
      "qty": 3,
      "amount": 300
    },
    "3": {
      "item_Seq_no": 3,
      "catalog_id": 177,
      "qty": 2,
      "amount": 200
    }
  }
}

我在下面写的代码并不完全按照上面的格式创建。

 let arr:NSMutableArray = []
    for i in 0...KCatalog.catalogValues.count-1
    {
        let seqnum1 = String(i + 1)
        let param2:[String: AnyObject] = [
            seqnum1: [
                "item_Seq_no"  : i+1,
                "catalog_id"    : ((KCatalog.catalogValues[i] as! catalogInfo).catalog_id),
                "qty"    : orderedQuantity[i],
                "amount"    : totalQuantityPrice[i],
            ],
        ]            
        arr.addObject(param2)
    }

    print(arr)

    let param1:NSMutableDictionary = [

        "cart_items"  : arr,
        "userID":"asdf",
        ]
    print(param1)
    let data1 = try! NSJSONSerialization.dataWithJSONObject(param1, options: NSJSONWritingOptions.PrettyPrinted)

    let json = String(data: data1, encoding: NSUTF8StringEncoding)
    if let json = json {
        print(json)
    }

我想知道给定的格式是否正确。如果是,请建议我以上述格式制作

我得到的输出是

{
  "userID": "asdf",
  "cart_items": [
    {
      "1": {
        "item_Seq_no": 1,
        "amount": 10,
        "catalog_id": "1",
        "qty": 1
      }
    },
    {
      "2": {
        "item_Seq_no": 2,
        "amount": 15,
        "catalog_id": "2",
        "qty": 1
      }
    },
    {
      "3": {
        "item_Seq_no": 3,
        "amount": 0,
        "catalog_id": "3",
        "qty": 0
      }
    }
  ]
}

根据 samantha 的回答,我得到了以下输出

    {
  "userID": "asdf",
  "cart_items": {
    "1": {
      "1": {
        "item_Seq_no": 1,
        "amount": 10,
        "catalog_id": "1",
        "qty": 1
      }
    },
    "2": {
      "2": {
        "item_Seq_no": 2,
        "amount": 15,
        "catalog_id": "2",
        "qty": 1
      }
    },
    "3": {
      "3": {
        "item_Seq_no": 3,
        "amount": 0,
        "catalog_id": "3",
        "qty": 0
      }
    }
  }
}

最佳答案

cart_items 应该是字典而不是数组。

var cartItems = [String: AnyObject]()
for i in 0...KCatalog.catalogValues.count-1
    {
        let seqnum1 = String(i + 1)
        let param2:[String: AnyObject] = [
                "item_Seq_no"  : i+1,
                "catalog_id"    : ((KCatalog.catalogValues[i] as! catalogInfo).catalog_id),
                "qty"    : orderedQuantity[i],
                "amount"    : totalQuantityPrice[i],
        ]            
        cartItems[seqnum1] = param2
    }

后来

let param1:NSMutableDictionary = [
        "cart_items"  : cartItems,
        "userID":"asdf",
        ]

关于ios - 以给定格式 Swift 创建 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41284581/

相关文章:

android - 使用Ayntask在Android中使用Listview解析Json

ios - 在不损失质量的情况下使 UIImage 成为一个圆圈? ( swift )

如果我尝试将其转换为其他任何内容,JSON 序列化将返回 nil

iOS:Alamofire 多部分图像提交

iphone - Monotouch UIScrollView,放大时文本模糊

iphone - 如何获取此 NSURL 的 ID 部分?

json - Electron编写json文件在dev中工作但在dist中不起作用

iOS - 向远程通知添加操作。我错过了什么?

ios - 使用 Swift 在目标之间共享 **文件**

python - 从数据框中删除列中的某些特定关键字并将其保存到 json