How to convert the given dictionary into a JSON Object containing a JSON Array In Swift?

Saswat Raj Pandey

I am really new to swift and was having trouble converting the dictionary I have into a JSON that is accepted by the api I am calling.

I have already created a dictionary with the relevant data and am converting it to JSON.

example:---

let requestbody = ["content": ["app_version_name": , "device_locale": , "device_model": , "device_id": , "os_run_time": , "device_time_zone": , "__type": "", "device_type": , "os_build_type": , "installation_id": "", "cloud_version": , "os_version": , "app_package_name": ]]

let jsonData = try? JSONSerialization.data(withJSONObject: requestBody)
let convertedString = String(data: jsonData!, encoding: String.Encoding.utf8)

But while printing the convertedString i am getting:

{
  "content": {
"installation_id": "",
"device_type": "",
"device_id": "",
"device_model": "",
"device_time_zone": "",
"cloud_version": "",
"os_run_time": "",
"device_locale": "",
"app_version_name": "",
"app_package_name": "",
"os_build_type": "",
"os_version": "",
"__type": ""
  }
}

The result I am expecting is

{
  "content": [
{
"installation_id": "",
"device_type": "",
"device_id": "",
"device_model": "",
"device_time_zone": "",
"cloud_version": "",
"os_run_time": "",
"device_locale": "",
"app_version_name": "",
"app_package_name": "",
"os_build_type": "",
"os_version": "",
"__type": ""
    }
 ]
}

Basically I need help in placing the values of content inside a jsonArray.

Rengers

Just wrap your entire content structure inside another array:

let content = ["app_version_name": "", "device_locale": "", "device_model": "", "device_id": "", "os_run_time": "", "device_time_zone": "", "__type": "", "device_type": "", "os_build_type": "", "installation_id": "", "cloud_version": "", "os_version": "", "app_package_name": ""]
let requestbody = ["content": [content]]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to convert array of dictionary to JSON?

How to convert the given json to json array

How to convert JavaScript object containing json string

swift: cast incoming json array to dictionary and object

Validate a JSON object containing a Dictionary

How to convert JSON to Dictionary in Swift 3 Vapor

How convert JSON file to dictionary in swift 4 ?

How to convert array object to JSON string . IOS 8 . Swift 1.2

How to convert JSON String to JSON Object in swift?

How to convert Array into json object?

How to convert array to a json object?

How to convert dictionary with numpy array to json and back?

How to convert String containing JSON object to actual JSON using Java

Convert Array model object into JSON - Swift 5

Convert Json Object to Array format in Swift 4

How to convert json Object to json Array?

Convert String containing list of JSON objects to Object Array

create a json object containing an array of json object

How to convert an array of JSON into an array of javascript object?

How to convert JSON array to OBJECT array javascript?

Convert dictionary to Javascript JSON Object?

Given an array of json objects, how do I convert values in each object to an int or float?

Decoding JSON with Swift containing dictionary with different types

How to convert NSObject class object into JSON in Swift?

How to convert an object to a serializable JSON in swift

How to convert a Swift object to a dictionary

Convert Python dictionary to a JSON array

Convert Python dictionary to JSON array

Convert Python dictionary to JSON array