2015-09-18 2 views
0

J'ai un problème. Quand j'essaye de faire tester mon json, il dit que ce n'est pas valide. Mais quand je le teste avec des services en ligne, il le lit bien.Impossible de sérialiser JSON Objectif-c

Le code que j'utilise pour tester si JSON est valide est:

NSString* stringRes = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 
[NSJSONSerialization isValidJSONObject:stringRes]; 

Où stringRes est une chaîne de fils au format NSString (De réponse de données) JSON.

{ 
"response": [ 
9, 
{ 
    "id": 47, 
    "from_id": 211551056, 
    "to_id": -101815039, 
    "date": 1442585775, 
    "post_type": "post", 
    "text": "Кто на андроид кодит хоть немножк ? Хотя бы hello world ?", 
    "can_edit": 1, 
    "created_by": 211551056, 
    "can_delete": 1, 
    "post_source": { 
    "type": "api", 
    "platform": "iphone" 
    }, 
    "comments": { 
    "count": 0, 
    "can_post": 1 
    }, 
    "likes": { 
    "count": 0, 
    "user_likes": 0, 
    "can_like": 1, 
    "can_publish": 0 
    }, 
    "reposts": { 
    "count": 0, 
    "user_reposted": 0 
    }, 
    "online": 1, 
    "reply_count": 0 
}, 
{ 
    "id": 43, 
    "from_id": 217702824, 
    "to_id": -101815039, 
    "date": 1442555658, 
    "post_type": "post", 
    "text": "Учебник по географии", 
    "can_delete": 1, 
    "attachment": { 
    "type": "doc", 
    "doc": { 
     "did": 419802605, 
     "owner_id": 217702824, 
     "title": "11_geo_m_2012.pdf", 
     "size": 92318316, 
     "ext": "pdf", 
     "url": "http://vk.com/doc217702824_419802605?hash=5eb9bb9753a6f88e88&dl=fcadca27db5845a002&api=1", 
     "access_key": "8e4218746055687cc2" 
    } 
    }, 
    "attachments": [ 
    { 
     "type": "doc", 
     "doc": { 
     "did": 419802605, 
     "owner_id": 217702824, 
     "title": "11_geo_m_2012.pdf", 
     "size": 92318316, 
     "ext": "pdf", 
     "url": "http://vk.com/doc217702824_419802605?hash=5eb9bb9753a6f88e88&dl=fcadca27db5845a002&api=1", 
     "access_key": "8e4218746055687cc2" 
     } 
    } 
    ], 
    "post_source": { 
    "type": "api", 
    "platform": "android" 
    }, 
    "comments": { 
    "count": 0, 
    "can_post": 1 
    }, 
    "likes": { 
    "count": 3, 
    "user_likes": 0, 
    "can_like": 1, 
    "can_publish": 0 
    }, 
    "reposts": { 
    "count": 0, 
    "user_reposted": 0 
    }, 
    "online": 0, 
    "reply_count": 0 
}, 
{ 
    "id": 37, 
    "from_id": 211551056, 
    "to_id": -101815039, 
    "date": 1442502437, 
    "post_type": "post", 
    "text": "У кого айфон, + в комменты", 
    "can_delete": 1, 
    "attachment": { 
    "type": "poll", 
    "poll": { 
     "poll_id": 195892301, 
     "question": "Удобно смотреть расписание ?(На сайте)" 
    } 
    }, 
    "attachments": [ 
    { 
     "type": "poll", 
     "poll": { 
     "poll_id": 195892301, 
     "question": "Удобно смотреть расписание ?(На сайте)" 
     } 
    } 
    ], 
    "post_source": { 
    "type": "api", 
    "platform": "iphone" 
    }, 
    "comments": { 
    "count": 5, 
    "can_post": 1 
    }, 
    "likes": { 
    "count": 0, 
    "user_likes": 0, 
    "can_like": 1, 
    "can_publish": 0 
    }, 
    "reposts": { 
    "count": 0, 
    "user_reposted": 0 
    }, 
    "online": 1, 
    "reply_count": 5 
}, 
{ 
    "id": 36, 
    "from_id": 249011483, 
    "to_id": -101815039, 
    "date": 1442500757, 
    "post_type": "post", 
    "text": "Домашку выложить не забудьте ", 
    "can_delete": 1, 
    "post_source": { 
    "type": "api", 
    "platform": "iphone" 
    }, 
    "comments": { 
    "count": 0, 
    "can_post": 1 
    }, 
    "likes": { 
    "count": 1, 
    "user_likes": 0, 
    "can_like": 1, 
    "can_publish": 0 
    }, 
    "reposts": { 
    "count": 0, 
    "user_reposted": 0 
    }, 
    "online": 1, 
    "reply_count": 0 
}, 
{ 
    "id": 28, 
    "from_id": 211551056, 
    "to_id": -101815039, 
    "date": 1442306249, 
    "post_type": "post", 
    "text": "", 
    "can_delete": 1, 
    "media": { 
    "type": "photo", 
    "owner_id": -101815039, 
    "item_id": 381477357, 
    "thumb_src": "http://cs624425.vk.me/v624425056/49e35/_w_YDirrrQU.jpg" 
    }, 
    "attachment": { 
    "type": "photo", 
    "photo": { 
     "pid": 381477357, 
     "aid": -8, 
     "owner_id": -101815039, 
     "user_id": 211551056, 
     "src": "http://cs624425.vk.me/v624425056/49e35/_w_YDirrrQU.jpg", 
     "src_big": "http://cs624425.vk.me/v624425056/49e36/439edT8aXks.jpg", 
     "src_small": "http://cs624425.vk.me/v624425056/49e34/uLxZj9yF1F4.jpg", 
     "src_xbig": "http://cs624425.vk.me/v624425056/49e37/nhvaG43cV-Y.jpg", 
     "src_xxbig": "http://cs624425.vk.me/v624425056/49e38/Ox54xM039hg.jpg", 
     "width": 960, 
     "height": 720, 
     "text": "", 
     "created": 1442306250, 
     "lat": 55.785025, 
     "long": 37.706605, 
     "post_id": 28, 
     "access_key": "454823aa95a2ee9fbc" 
    } 
    }, 
    "attachments": [ 
    { 
     "type": "photo", 
     "photo": { 
     "pid": 381477357, 
     "aid": -8, 
     "owner_id": -101815039, 
     "user_id": 211551056, 
     "src": "http://cs624425.vk.me/v624425056/49e35/_w_YDirrrQU.jpg", 
     "src_big": "http://cs624425.vk.me/v624425056/49e36/439edT8aXks.jpg", 
     "src_small": "http://cs624425.vk.me/v624425056/49e34/uLxZj9yF1F4.jpg", 
     "src_xbig": "http://cs624425.vk.me/v624425056/49e37/nhvaG43cV-Y.jpg", 
     "src_xxbig": "http://cs624425.vk.me/v624425056/49e38/Ox54xM039hg.jpg", 
     "width": 960, 
     "height": 720, 
     "text": "", 
     "created": 1442306250, 
     "lat": 55.785025, 
     "long": 37.706605, 
     "post_id": 28, 
     "access_key": "454823aa95a2ee9fbc" 
     } 
    } 
    ], 
    "post_source": { 
    "type": "api", 
    "platform": "iphone" 
    }, 
    "comments": { 
    "count": 0, 
    "can_post": 1 
    }, 
    "likes": { 
    "count": 1, 
    "user_likes": 0, 
    "can_like": 1, 
    "can_publish": 0 
    }, 
    "reposts": { 
    "count": 0, 
    "user_reposted": 0 
    }, 
    "online": 1, 
    "reply_count": 0 
}, 
{ 
    "id": 27, 
    "from_id": 211551056, 
    "to_id": -101815039, 
    "date": 1442306164, 
    "post_type": "post", 
    "text": "", 
    "can_delete": 1, 
    "media": { 
    "type": "photo", 
    "owner_id": -101815039, 
    "item_id": 381477244, 
    "thumb_src": "http://cs624425.vk.me/v624425056/49e2c/kttj5pUpjTk.jpg" 
    }, 
    "attachment": { 
    "type": "photo", 
    "photo": { 
     "pid": 381477244, 
     "aid": -8, 
     "owner_id": -101815039, 
     "user_id": 211551056, 
     "src": "http://cs624425.vk.me/v624425056/49e2c/kttj5pUpjTk.jpg", 
     "src_big": "http://cs624425.vk.me/v624425056/49e2d/ihg9szTvLew.jpg", 
     "src_small": "http://cs624425.vk.me/v624425056/49e2b/GpS0xwCa66c.jpg", 
     "src_xbig": "http://cs624425.vk.me/v624425056/49e2e/pe4tflJokqs.jpg", 
     "src_xxbig": "http://cs624425.vk.me/v624425056/49e2f/7vkU8nUyNxc.jpg", 
     "width": 960, 
     "height": 720, 
     "text": "", 
     "created": 1442306166, 
     "lat": 55.785083, 
     "long": 37.711116, 
     "post_id": 27, 
     "access_key": "cb01ac011f4abbc0af" 
    } 
    }, 
    "attachments": [ 
    { 
     "type": "photo", 
     "photo": { 
     "pid": 381477244, 
     "aid": -8, 
     "owner_id": -101815039, 
     "user_id": 211551056, 
     "src": "http://cs624425.vk.me/v624425056/49e2c/kttj5pUpjTk.jpg", 
     "src_big": "http://cs624425.vk.me/v624425056/49e2d/ihg9szTvLew.jpg", 
     "src_small": "http://cs624425.vk.me/v624425056/49e2b/GpS0xwCa66c.jpg", 
     "src_xbig": "http://cs624425.vk.me/v624425056/49e2e/pe4tflJokqs.jpg", 
     "src_xxbig": "http://cs624425.vk.me/v624425056/49e2f/7vkU8nUyNxc.jpg", 
     "width": 960, 
     "height": 720, 
     "text": "", 
     "created": 1442306166, 
     "lat": 55.785083, 
     "long": 37.711116, 
     "post_id": 27, 
     "access_key": "cb01ac011f4abbc0af" 
     } 
    } 
    ], 
    "post_source": { 
    "type": "api", 
    "platform": "iphone" 
    }, 
    "comments": { 
    "count": 0, 
    "can_post": 1 
    }, 
    "likes": { 
    "count": 1, 
    "user_likes": 0, 
    "can_like": 1, 
    "can_publish": 0 
    }, 
    "reposts": { 
    "count": 0, 
    "user_reposted": 0 
    }, 
    "online": 1, 
    "reply_count": 0 
}, 
{ 
    "id": 16, 
    "from_id": 249011483, 
    "to_id": -101815039, 
    "date": 1442069345, 
    "post_type": "post", 
    "text": "Кто нибудь сделал алгебру ?", 
    "can_delete": 1, 
    "post_source": { 
    "type": "api", 
    "platform": "iphone" 
    }, 
    "comments": { 
    "count": 6, 
    "can_post": 1 
    }, 
    "likes": { 
    "count": 3, 
    "user_likes": 0, 
    "can_like": 1, 
    "can_publish": 0 
    }, 
    "reposts": { 
    "count": 0, 
    "user_reposted": 0 
    }, 
    "online": 1, 
    "reply_count": 6 
}, 
{ 
    "id": 9, 
    "from_id": 127082413, 
    "to_id": -101815039, 
    "date": 1441911418, 
    "post_type": "post", 
    "text": "Официальная информациия ,староста нашей группы Александр Кондор!", 
    "can_delete": 1, 
    "post_source": { 
    "type": "api", 
    "platform": "android" 
    }, 
    "comments": { 
    "count": 6, 
    "can_post": 1 
    }, 
    "likes": { 
    "count": 2, 
    "user_likes": 0, 
    "can_like": 1, 
    "can_publish": 0 
    }, 
    "reposts": { 
    "count": 0, 
    "user_reposted": 0 
    }, 
    "online": 1, 
    "reply_count": 6 
}, 
{ 
    "id": 1, 
    "from_id": -101815039, 
    "to_id": -101815039, 
    "date": 1441656588, 
    "post_type": "post", 
    "text": "", 
    "can_delete": 1, 
    "can_pin": 1, 
    "media": { 
    "type": "photo", 
    "owner_id": -101815039, 
    "item_id": 380592775, 
    "thumb_src": "http://cs629111.vk.me/v629111548/e99f/DcV1yHZK1fw.jpg" 
    }, 
    "attachment": { 
    "type": "photo", 
    "photo": { 
     "pid": 380592775, 
     "aid": -6, 
     "owner_id": -101815039, 
     "user_id": 100, 
     "src": "http://cs629111.vk.me/v629111548/e99f/DcV1yHZK1fw.jpg", 
     "src_big": "http://cs629111.vk.me/v629111548/e9a0/Qf8q8PpH7Jg.jpg", 
     "src_small": "http://cs629111.vk.me/v629111548/e99e/2Jq8e-0SptQ.jpg", 
     "src_xbig": "http://cs629111.vk.me/v629111548/e9a1/Uha0MvOoVCU.jpg", 
     "src_xxbig": "http://cs629111.vk.me/v629111548/e9a2/v8hqUldA6Kk.jpg", 
     "width": 730, 
     "height": 1024, 
     "text": "", 
     "created": 1441656588, 
     "post_id": 1 
    } 
    }, 
    "attachments": [ 
    { 
     "type": "photo", 
     "photo": { 
     "pid": 380592775, 
     "aid": -6, 
     "owner_id": -101815039, 
     "user_id": 100, 
     "src": "http://cs629111.vk.me/v629111548/e99f/DcV1yHZK1fw.jpg", 
     "src_big": "http://cs629111.vk.me/v629111548/e9a0/Qf8q8PpH7Jg.jpg", 
     "src_small": "http://cs629111.vk.me/v629111548/e99e/2Jq8e-0SptQ.jpg", 
     "src_xbig": "http://cs629111.vk.me/v629111548/e9a1/Uha0MvOoVCU.jpg", 
     "src_xxbig": "http://cs629111.vk.me/v629111548/e9a2/v8hqUldA6Kk.jpg", 
     "width": 730, 
     "height": 1024, 
     "text": "", 
     "created": 1441656588, 
     "post_id": 1 
     } 
    } 
    ], 
    "post_source": { 
    "type": "api", 
    "platform": "android", 
    "data": "profile_photo" 
    }, 
    "comments": { 
    "count": 0, 
    "can_post": 1 
    }, 
    "likes": { 
    "count": 0, 
    "user_likes": 0, 
    "can_like": 1, 
    "can_publish": 0 
    }, 
    "reposts": { 
    "count": 0, 
    "user_reposted": 0 
    }, 
    "online": 1, 
    "reply_count": 0 
} 
] 
} 

UPD: Résolu en utilisant

NSData* data = [stringRes dataUsingEncoding:NSUTF8StringEncoding]; 

Répondre

1

isValidJSONObject teste si un objet JSON (un NSDictionary ou un NSA rray) peut être converti avec succès en données JSON.

Il ne s'agit pas de tester si un objet NSData contient des données JSON valides. Pour tester les données JSON valide, vous appelez simplement

[NSJSONSerialization JSONObjectWithData: données ...]

et vérifier si la valeur de retour est nul ou non.

+0

Heh ... Je l'ai déjà utilisé et ça a cassé mon application ... Puis j'ai pensé à le tester. –

+0

NSArray * dict = [NSJSONSerialization JSONObjectWithData: options stringRes: erreur NSJSONReadingMutableLeaves: & error]; –