Create JSON in given format Swift

100 views Asked by At

I want to create a JSON in below format.

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

Code i written in below is not exactly creating in above format.

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

I want to whether given format is correct or not. If yes please suggest me to make it in above format

Output i am getting is

{
  "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
      }
    }
  ]
}

According to samantha answer i got below output

    {
  "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
      }
    }
  }
}
2

There are 2 answers

0
Samantha On BEST ANSWER

cart_items should be a dictionary rather than an array.

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
    }

And then later

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

Based on @Samantha answer here below is the Swift 3.0 code.

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

    print(cartDictionaryArr)

    let finalDict:NSMutableDictionary = [
        "cart_items"  : cartDictionaryArr,
        "userID":"123",
        ]

    print(finalDict)
    let data1 = try! JSONSerialization.data(withJSONObject: finalDict, options: JSONSerialization.WritingOptions.prettyPrinted)

    let json = String(data: data1, encoding: String.Encoding.utf8)
    if let json = json {
        print(json)
    }

Here is the Swift 2 working Code -

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

       print(cartDictionaryArr)

       let finalDict:NSMutableDictionary = [
         "cart_items"  : cartDictionaryArr,
         "userID":"123",
       ]
       print(finalDict)

       let data1 = try! NSJSONSerialization.dataWithJSONObject(finalDict, options: NSJSONWritingOptions.PrettyPrinted)

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