我需要像这样创建JSON:
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' };
问题是我无法创建有效的JSON。
这是我如何创建对象:
let jsonObject: [AnyObject] = [ ["type_id": singleStructDataOfCar.typeID,"model_id": singleStructDataOfCar.modelID,"transfer": savedDataTransfer,"hourly": savedDataHourly,"custom": savedDataReis,"device_type":"ios"] ]
其中savedData是字典:
let savedData: NSDictionary = ["ServiceDataStartDate": singleStructdata.startofWork,"ServiceDataAddressOfReq": singleStructdata.addressOfRequest,"ServiceDataAddressOfDel": singleStructdata.addressOfDelivery,"ServiceDataDetailedText": singleStructdata.detailedText,"ServiceDataPrice": singleStructdata.priceProposed]
当我仅使用创建我的JSON对象的字符串时,一切正常。但是当我包含字典NSJSONSerialization.isValidJSONObject(value)返回false。如何创建有效的字典?
一个问题是这个代码不是类型字典。
原文链接:https://www.f2er.com/swift/320466.htmllet jsonObject: [AnyObject] = [ [ "type_id": singleStructDataOfCar.typeID,"device_type":"iOS" ] ]
以上是一个AnyObject的数组,其中包含一个类型为[String:AnyObject]的字典。
尝试这样的方式来匹配上面提供的JSON:
let savedData = ["Something": 1] let jsonObject: [String: AnyObject] = [ "type_id": 1,"model_id": 1,"transfer": [ "startDate": "10/04/2015 12:45","endDate": "10/04/2015 16:00" ],"custom": savedData ] let valid = NSJSONSerialization.isValidJSONObject(jsonObject) // true