Here is the code for the below Json output:
let params : [[String : AnyObject]] = [["name" : "action", "value" : "pay" ],["name" : "cartJsonData" , "value" : ["total": 1,"rows":[["quantity": “1” ,"title":"Donation for SMSF India - General Fund","price":"1","itemId":"DN001","cost": “1”,”currency":"INR"]]]], ["name" : "center", "value" : "Chennai"], ["name" : "flatNumber", "value" : "503"], ["name" : "panNumber", "value" : ""], ["name" : "payWith"], ["name" : "reminderFrequency","value" : "Monthly"], ["name" : "shipToAddr1"], ["name" : "shipToAddr2"], ["name" : "shipToCity"], ["name" : "shipToCountryName" , "value" : "India"], ["name" : "shipToEmail", "value" : “[email protected]"], ["name" : "shipToFirstName" , "value": "4480101010"], ["name" : "shipToLastName"], ["name" : "shipToPhone", "value" : "4480101010"], ["name" : "shipToState"], ["name" : "shipToZip"], ["name" : "userId", "value" : “null”], ["name" : "shipToCountry", "value" : "IN"]] var jsonObject: NSData? = nil do { jsonObject = try NSJSONSerialization.dataWithJSONObject(params, options: NSJSONWritingOptions()) print(jsonObject) // This will print the below json. } catch{}
By printing jsonObject, I got this one.
[{ "value": "pay", "name": "action" }, { "value": { "rows": [{ "price": "1", "quantity": "1", "cost": "1", "currency": "INR", "itemId": "DN001", "title": "Donation for SMSF India - General Fund" }], "total": 1 }, "name": "cartJsonData" }, { "value": "Chennai", "name": "center" }, { "value": "503", "name": "flatNumber" }, { "value": "", "name": "panNumber" }, { "name": "payWith" }, { "value": "Monthly", "name": "reminderFrequency" }, { "name": "shipToAddr1" }, { "name": "shipToAddr2" }, { "name": "shipToCity" }, { "value": "India", "name": "shipToCountryName" }, { "value": "[email protected]", "name": "shipToEmail" }, { "value": "4480101010", "name": "shipToFirstName" }, { "name": "shipToLastName" }, { "value": "4480101010", "name": "shipToPhone" }, { "name": "shipToState" }, { "name": "shipToZip" }, { "value": "null", "name": "userId" }, { "value": "IN", "name": "shipToCountry" }]
And I want the JSON to be in the below format.
[{ “name”: “action”, “value”: “pay” }, { “name”: “cartJsonData”, “value”: “{\”total\”:1,\”rows\”:[{\”itemId\”:\”DN002\”,\”title\”:\”Donation for SMSF India - General Fund\”,\”quantity\”:\”100\”,\”currency\”:\”INR\”,\”price\”:\”1\”,\”cost\”:\”100\”}]}” }, { “name”: “center”, “value”: “Chennai” }, { “name”: “flatNumber”, “value”: “ “ }, { “name”: “panNumber”, “value”: “ASSDDBBDJD” }, { “name”: “payWith” }, { “name”: “reminderFrequency”, “value”: “Monthly” }, { “name”: “shipToAddr1” }, { “name”: “shipToAddr2” }, { “name”: “shipToCity” }, { “name”: “shipToCountryName”, “value”: “India” }, { “name”: “shipToEmail”, “value”: “[email protected]” }, { “name”: “shipToFirstName”, “value”: “Raju” }, { “name”: “shipToLastName” }, { “name”: “shipToPhone”, “value”: “1234567890” }, { “name”: “shipToState” }, { “name”: “shipToZip” }, { “name”: “userId”, “value”: “null” }, { “name”: “shipToCountry”, “value”: “IN” }]
How can it be done? Only the value in cartJsonData
needs to be changed. Can someone help me on this to solve it?
An object that converts between JSON and the equivalent Foundation objects.
Swift JSON ParsingJSON stands for JavaScript Object Notation. It's a popular text-based data format used everywhere for representing structured data. Almost every programming language supports it with Swift being no exception. You are going to use JSON a lot throughout your career, so make sure you don't miss out.
Try this.
func jsonToString(json: AnyObject){ do { let data1 = try NSJSONSerialization.dataWithJSONObject(json, options: NSJSONWritingOptions.PrettyPrinted) // first of all convert json to the data let convertedString = String(data: data1, encoding: NSUTF8StringEncoding) // the data will be converted to the string print(convertedString) // <-- here is ur string } catch let myJSONError { print(myJSONError) } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With