无法序列化 JSON Objective-c

Posted

技术标签:

【中文标题】无法序列化 JSON Objective-c【英文标题】:Fail to serialize JSON Objective-c 【发布时间】:2015-09-18 20:10:35 【问题描述】:

我有问题。当我尝试测试我的 json 时,它说它无效。但是当我用在线服务测试它时,它读起来很好。

我用来测试json是否有效的代码是:

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

其中 stringRes 是 NSString 格式的子字符串。(来自数据响应) 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:通过使用解决

NSData* data = [stringRes dataUsingEncoding:NSUTF8StringEncoding];

【问题讨论】:

【参考方案1】:

isValidJSONObject 测试 JSON 对象(NSDictionary 或 NSArray)是否可以成功转换为 JSON 数据。

它不是用于测试 NSData 对象是否包含有效的 JSON 数据。要测试有效的 JSON 数据,您只需调用

[NSJSONSerialization JSONObjectWithData:data ...]

并检查返回值是否为零。

【讨论】:

嘿...我以前用过它,它破坏了我的应用程序...然后我想测试它。 NSArray* dict = [NSJSONSerialization JSONObjectWithData:stringRes options:NSJSONReadingMutableLeaves error:&error];

以上是关于无法序列化 JSON Objective-c的主要内容,如果未能解决你的问题,请参考以下文章

反序列化 Json 出现错误“无法反序列化当前 JSON 数组”

无法反序列化 JSON 响应

无法反序列化 JSON 内容

无法反序列化当前的JSON对象,为啥

无法反序列化 text/html json 响应

json_serializable 无法反序列化