Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create JSON in swift

Tags:

json

object

swift

I need to create JSON like this:

Order = {   type_id:'1',model_id:'1',     transfer:{      startDate:'10/04/2015 12:45',      endDate:'10/04/2015 16:00',      startPoint:'Ул. Момышулы, 45',      endPoint:'Аэропорт Астаны'    },    hourly:{      startDate:'10/04/2015',      endDate:'11/04/2015',      startPoint:'ЖД Вокзал',      endPoint:'',      undefined_time:'1'    },    custom:{      startDate:'12/04/2015',      endDate:'12/04/2015',      startPoint:'Астана',      endPoint:'Павлодар',      customPrice:'50 000'    },     commentText:'',     device_type:'ios' }; 

The problem is that I can not create valid JSON. Here is how I create object:

let jsonObject: [AnyObject]  = [         ["type_id": singleStructDataOfCar.typeID, "model_id": singleStructDataOfCar.modelID, "transfer": savedDataTransfer, "hourly": savedDataHourly, "custom": savedDataReis, "device_type":"ios"]     ] 

where savedData are dictionaries:

let savedData: NSDictionary = ["ServiceDataStartDate": singleStructdata.startofWork,  "ServiceDataAddressOfReq": singleStructdata.addressOfRequest,  "ServiceDataAddressOfDel": singleStructdata.addressOfDelivery,  "ServiceDataDetailedText": singleStructdata.detailedText, "ServiceDataPrice": singleStructdata.priceProposed] 

When I use only strings creating my JSON object everything works fine. However when I include dictionaries NSJSONSerialization.isValidJSONObject(value) returns false. How can I create a valid dictionary?

like image 340
Yestay Muratov Avatar asked Apr 08 '15 11:04

Yestay Muratov


People also ask

What is JSON format in Swift?

Swift JSON Parsing JSON 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.

What is JSON serialization in Swift?

Swift version: 5.6. If you want to parse JSON by hand rather than using Codable , iOS has a built-in alternative called JSONSerialization and it can convert a JSON string into a collection of dictionaries, arrays, strings and numbers in just a few lines of code.


1 Answers

One problem is that this code is not of type Dictionary.

let jsonObject: [Any]  = [     [          "type_id": singleStructDataOfCar.typeID,          "model_id": singleStructDataOfCar.modelID,           "transfer": savedDataTransfer,           "hourly": savedDataHourly,           "custom": savedDataReis,           "device_type":"iOS"     ] ] 

The above is an Array of AnyObject with a Dictionary of type [String: AnyObject] inside of it.

Try something like this to match the JSON you provided above:

let savedData = ["Something": 1]  let jsonObject: [String: Any] = [      "type_id": 1,     "model_id": 1,     "transfer": [         "startDate": "10/04/2015 12:45",         "endDate": "10/04/2015 16:00"     ],     "custom": savedData ]  let valid = JSONSerialization.isValidJSONObject(jsonObject) // true 
like image 99
Matt Mathias Avatar answered Oct 08 '22 18:10

Matt Mathias