2016-12-22 9 views
0

서버에 json 객체를 보내려고합니다. 서버는 객체가이 형식이 될 것으로 기대 :신속한 잘못된 json 형식

[ 
"Lat": 10.33688590000001, 
"Name": nameOfSomething, //this is missing "" 
"Lng": 58.43135800000005 
] 

내가 사용하는 코드는 이것이다 :

{ 
"Lat": 10.33688590000001, 
"Name": "nameOfSomething", 
"Lng": 58.43135800000005 
} 

하지만 난 개체를 정의한 후지고있어 목적은 이것이다

let jsonObject: [String: AnyObject] = [ 

    "Lat": lat, 
    "Name": nameOfSender.text!, 
    "Lng": lng 
    ] 


let jsonData = try! NSJSONSerialization.dataWithJSONObject(jsonObject, options: .PrettyPrinted) 

어떤 아이디어가이 문제를 해결할 수 있습니까?

+0

서버에 JSON을 보내려면'.PrettyPrinted' 대신'[]'옵션을 사용하지 마십시오. – Moritz

+0

@Eric Aya - 작동하지 않았습니다. 그 오류는 어딘가에 거짓말을한다고 생각합니다. "jsonObject : [String : AnyObject] = ["객체를 정의하는 행 "] – markan3

+0

정말로 서버에 보내고 있습니까? 아니면 문제가 있다고 생각하기 때문에 이전에 멈추고 있습니까? 나는 당신의 코드에서 서버에 문제를 일으킬 수있는 것을 보지 못한다. (꽤 인쇄 된 것을 제외하고는 이것이 이유가 아니다). 꽤 인쇄되지 않은 JSON 데이터를보고 보내는 것에 의지하지 마십시오. 어떻게됩니까? 오류 메시지 란 무엇입니까? – Moritz

답변

0
Please Look on this. it will give you correct answer. 
    /// Use this method to get JSON string from Dictionary. 
    /// 
    /// 
    /// - returns: `String` for Dictionary 
    func getJSONStringForDictionary(dict:[String:AnyObject]) -> String { 
    do { 
     let jsonData = try NSJSONSerialization.dataWithJSONObject(dict, options: []) 
     if let jsonString = NSString(data: jsonData, encoding: NSUTF8StringEncoding) as? String { 
     return jsonString 
     } 
     // here "jsonData" is the dictionary encoded in JSON data 
    } catch let error as NSError { 
     print(error) 
    } 
    return "" 
    } 


/* calling */ 


let json = self.getJSONStringForDictionary(jsonObject) 


    print(json) 
//{ 
    "Lat" : 10.33688590000001, 
    "Name" : "nameOfSomething", 
    "Lng" : 58.43135800000005 
}