创建JSON对象时遇到问题。我的JSON如下所示
{
"user":{
"accountId": 0,
"active": false,
"createdBy": 0,
"createdOn": null,
"emailAddress": null,
"firstName": null,
"gender": 0,
"id": 9,
"lastName": null,
"role": 5
},
"InLocation": "",
"InLatitude": 18.5081214,
"InLangitude": 73.8431377,
"OutLocation": "",
"OutLatitude": 0,
"OutLagitude": 0,
"InLocationNotFoundReason": "NetworkProblem",
"OutLocationNotFoundReason": "",
"sdate": "2014-12-10",
"spunchInTime": "08:37:44"
}
我想发布这个json对象。但我无法将此JSON转换为数组并将其传递给NSURLRequest。我使用如下所示,但它没有工作:
let params:[String: AnyObject] = ["user":["accountId":1, "active":false, "createdBy":0, "createdOn":"null", "emailAddress":"ramesh.golla@qlc.in", "firstName":"Ramesh", "gender":1, "id":0, "lastName":"Golla", "role":0], "punchInLocation":"null","punchInLatitude":0.0, "punchInLangitude":0.0, "punchOutLocation":"Unknown Location", "punchOutLatitude":0.0, "punchOutLagitude":0.0, "punchInLocationNotFoundReason":"NetworkProblem", "punchOutLocationNotFoundReason":"", "sdate":"2015-05-08", "spunchInTime":"10:02:53"]
答案 0 :(得分:1)
最简单的方法是将所有内容放入字典中,并使用NSJSONSerialization将其转换为NSData。我不太明白为什么你想把它变成一个数组,你的NSURLRequest会想要NSData。
我想知道为什么你这里有一个字符串。 JSON数据应该作为NSData,或作为字典或数组。