JSONのパースに失敗する
iPhoneアプリを作り始めたのですが、JSONのパースがうまくいかずに非常に困っています。
JSONは下記のような並びになります。
{
"status":"success",
"comments":[
[
{
"comments_id":"12",
"comments_is": true
}
],
[
{
"comments_id":"12",
"comments_is": true
}
]
],
"documents":[
{
"documents_id":"22",
"documents_date":"221",
"share": ["22","19"]
},
{
"documents_id":"22",
"documents_date":"221",
"share": ["22","19"]
}
{
"documents_id":"22",
"documents_date":"221"
}
],
"likes":[
[
{
"liked_id":"42",
"liked_user":"jj"
}
],
[
{
"liked_id":"42",
"liked_user":"jj"
}
],
[
{
"liked_id":"42",
"liked_user":"jj"
}
]
]
}
ネットで調べると主に下記のようなコードが載っているのを見かけましたので試してみましたがなかなかうまくいかず、catchに入ってしまいます。
do {
let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String:AnyObject]
print(json?["documents"] ?? "none")
} catch {
print ("json error")
return
}
ちなみにJSONの並びを「status」や「comments」だけにした場合ですと、下記のコードで取得できました。
do {
let json = try JSONSerialization.jsonObject(with: data, options: []) as? Dictionary<String, AnyObject>
print(json?["status"] ?? "none")
} catch {
print ("json error")
return
}
「documents」は特に全く所得がうまくいきません。
お詳しい方いましたらどうぞよろしくお願いします。
Swift3
xcode 8.2.1
※コード追記しました
上記のJSONコードが手書きで書いたため、正しく文になっていなかったので、実際のJSON文字列をほぼコピペして記載しました。
下記が正しいJSONになります。
{
"status": "success",
"comments": [
[
],
[
{
"comments_id": "12",
"comments_documents_id": "434",
"comments_user_id": "68",
"comments_comment": "また",
"comments_created_at": "2017-01-02 12:59:38",
"comments_user_name_id": "kur",
"comment_count": "1"
}
],
[
],
[
],
[
],
[
],
[
{
"comments_id": "9",
"comments_documents_id": "429",
"comments_user_id": "69",
"comments_comment": "いいね!",
"comments_created_at": "2016-12-30 16:34:31",
"comments_user_name_id": "ho",
"comment_count": "3"
},
{
"comments_id": "10",
"comments_documents_id": "429",
"comments_user_id": "67",
"comments_comment": "ありがとうございます!",
"comments_created_at": "2016-12-30 16:35:18",
"comments_user_name_id": "ne",
"comment_count": "3"
},
{
"comments_id": "11",
"comments_documents_id": "429",
"comments_user_id": "67",
"comments_comment": "こす",
"comments_created_at": "2016-12-30 16:36:37",
"comments_user_name_id": "ne",
"comment_count": "3"
}
]
],
"documents": [
{
"documents_id": "449",
"documents_user_id": "68",
"documents_post_type": "1",
"documents_photo_path": "",
"documents_message": "たか",
"documents_private_flag": "0",
"documents_fence_id": "128",
"documents_timer": "0",
"documents_created_at": "20170118220505",
"documents_distance": 1,
"follow": "true",
"user_id": "68",
"user_nickname": "く",
"user_name_id": "ku",
"user_icon_path": "user_icon_img.png",
"user_official": "0",
"user_account_private": "false",
"action_user_nickname": "",
"action_user_name_id": "",
"action_user_id": "",
"action_target_user_id": "",
"action_created_at": "20170118220505",
"action": ""
},
{
"documents_id": "434",
"documents_user_id": "69",
"documents_post_type": "314",
"documents_photo_path": "6920170102124729.jpg",
"documents_message": "またあ",
"documents_private_flag": "0",
"documents_fence_id": "112",
"documents_timer": "0",
"documents_created_at": "20170102124732",
"documents_distance": 1,
"follow": "true",
"user_id": "69",
"user_nickname": "ひ",
"user_name_id": "hi",
"user_icon_path": "user_icon_img.png",
"user_official": "0",
"user_account_private": "false",
"action_user_nickname": "",
"action_user_name_id": "",
"action_user_id": "",
"action_target_user_id": "",
"action_created_at": "20170102124732",
"action": ""
},
{
"documents_id": "433",
"documents_user_id": "67",
"documents_post_type": "2",
"documents_photo_path": "6720161230173125.jpg",
"documents_message": "たかしまや",
"documents_private_flag": "0",
"documents_fence_id": "111",
"documents_timer": "0",
"documents_created_at": "20161230173126",
"documents_distance": 1,
"follow": "me",
"user_id": "67",
"user_nickname": "ねこ",
"user_name_id": "neko",
"user_icon_path": "user_icon_img.png",
"user_official": "1",
"user_account_private": "true",
"action_user_nickname": "",
"action_user_name_id": "",
"action_user_id": "",
"action_target_user_id": "",
"action_created_at": "20161230173126",
"action": ""
},
{
"documents_id": "432",
"documents_user_id": "67",
"documents_post_type": "2",
"documents_photo_path": "6720161230173048.jpg",
"documents_message": "こやて",
"documents_private_flag": "0",
"documents_fence_id": "111",
"documents_timer": "0",
"documents_created_at": "20161230173049",
"documents_distance": 1,
"follow": "me",
"user_id": "67",
"user_nickname": "ねこ",
"user_name_id": "neko",
"user_icon_path": "user_icon_img.png",
"user_official": "1",
"user_account_private": "true",
"action_user_nickname": "",
"action_user_name_id": "",
"action_user_id": "",
"action_target_user_id": "",
"action_created_at": "20161230173049",
"action": ""
},
{
"documents_id": "431",
"documents_user_id": "67",
"documents_post_type": "2",
"documents_photo_path": "6720161230173021.jpg",
"documents_message": "黒",
"documents_private_flag": "1",
"documents_fence_id": "111",
"documents_timer": "0",
"documents_created_at": "20161230173022",
"documents_distance": 1,
"follow": "me",
"user_id": "67",
"user_nickname": "ねこ",
"user_name_id": "neko",
"user_icon_path": "user_icon_img.png",
"user_official": "1",
"user_account_private": "true",
"action_user_nickname": "",
"action_user_name_id": "",
"action_user_id": "",
"action_target_user_id": "",
"action_created_at": "20161230173022",
"action": "",
"share_user": [
"72",
"68"
]
},
{
"documents_id": "430",
"documents_user_id": "69",
"documents_post_type": "2",
"documents_photo_path": "6920161230163950.jpg",
"documents_message": "ビット",
"documents_private_flag": "0",
"documents_fence_id": "112",
"documents_timer": "0",
"documents_created_at": "20161230163950",
"documents_distance": 1,
"follow": "true",
"user_id": "69",
"user_nickname": "ひ",
"user_name_id": "hi",
"user_icon_path": "user_icon_img.png",
"user_official": "0",
"user_account_private": "false",
"action_user_nickname": "",
"action_user_name_id": "",
"action_user_id": "",
"action_target_user_id": "",
"action_created_at": "20161230163950",
"action": ""
},
{
"documents_id": "429",
"documents_user_id": "67",
"documents_post_type": "2",
"documents_photo_path": "6720161230162438.jpg",
"documents_message": "てすと",
"documents_private_flag": "0",
"documents_fence_id": "111",
"documents_timer": "0",
"documents_created_at": "20161230162438",
"documents_distance": 2,
"follow": "me",
"user_id": "67",
"user_nickname": "ねこ",
"user_name_id": "ne",
"user_icon_path": "user_icon_img.png",
"user_official": "1",
"user_account_private": "true",
"action_user_nickname": "",
"action_user_name_id": "",
"action_user_id": "",
"action_target_user_id": "",
"action_created_at": "20161230162438",
"action": ""
}
],
"likes": [
[
],
[
{
"likes_id": "42",
"likes_documents_id": "434",
"likes_user_id": "68",
"likes_created_at": "2017-01-02 12:59:43",
"likes_user_name_id": "kuri",
"likes_count": "1"
}
],
[
{
"likes_id": "45",
"likes_documents_id": "433",
"likes_user_id": "77",
"likes_created_at": "2017-02-15 16:24:34",
"likes_user_name_id": "kuro",
"likes_count": "1"
}
],
[
{
"likes_id": "46",
"likes_documents_id": "432",
"likes_user_id": "77",
"likes_created_at": "2017-03-09 18:35:18",
"likes_user_name_id": "kuro",
"likes_count": "1"
}
],
[
],
[
],
[
{
"likes_id": "40",
"likes_documents_id": "429",
"likes_user_id": "67",
"likes_created_at": "2016-12-30 16:56:19",
"likes_user_name_id": "neko",
"likes_count": "1"
}
]
],
"share": ""
}
※もう少し全体のコードを追記しました。
func getPostListsFromMainTimeline() {
// 通信用のConfigを生成.
let config:URLSessionConfiguration = URLSessionConfiguration.background(withIdentifier: "backgroundTask")
// Sessionを生成.
let session: URLSession = URLSession(configuration: config, delegate: self, delegateQueue: nil)
// 通信先のURLを生成.
let url: NSURL = NSURL(string: "http://xxx.xx.xxx.xx/cl/list.php")!
// POST用のリクエストを生成.
let request: NSMutableURLRequest = NSMutableURLRequest(url: url as URL)
// POSTのメソッドを指定.
request.httpMethod = "POST"
// 送信するデータを生成・リクエストにセット.
let str: NSString = "NAME_ID=\("ne")&TOKEN_KEY=\("1309134774583ed")&TIMELINE=\(currentTimeline)" as NSString
let myData: NSData = str.data(using: String.Encoding.utf8.rawValue)! as NSData
request.httpBody = myData as Data
// タスクの生成.
let task: URLSessionDataTask = session.dataTask(with: request as URLRequest)
// タスクの実行.
task.resume()
}
public func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
do {
let json = try JSONSerialization.jsonObject(with: data, options: []) as? Dictionary<String, AnyObject>
print(json?["documents"] ?? "ddd")
} catch {
print ("json error")
return
}
}