0

I have a problem. When I try to make test my json, it says that it is not valid. But when I test it with online services, it reads it well.

The code I use to test if json is valid is:

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

Where stringRes is son string in NSString format.(From data response) 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: Solved by using

NSData* data = [stringRes dataUsingEncoding:NSUTF8StringEncoding];

1 Answer 1

1

isValidJSONObject tests if a JSON object (a NSDictionary or NSArray) can be successfully converted to JSON data.

It is not for testing if an NSData object contains valid JSON data. To test for valid JSON data you just call

[NSJSONSerialization JSONObjectWithData:data ...]

and check if the return value is nil or not.

Sign up to request clarification or add additional context in comments.

2 Comments

Heh... I used that before and it broke my app... Then I thought to test it.
NSArray* dict = [NSJSONSerialization JSONObjectWithData:stringRes options:NSJSONReadingMutableLeaves error:&error];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.