MIrfan
MIrfan

Reputation: 123

NSJSONSerialization return null

The code listed below return 4 items very good but when i increase the number of items say limit 5 then it return null, can someone told me why its behaving like this?

NSString *hostStr = @"http://localhost:8888/iphone-so/product.json.php?";
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:hostStr]];
NSError *error;
product = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

product.json.php return the following with 4 items

{
    "products": {
        "19": [
            {
                "id": "19",
                "name": "Save $240 on your next photo session",
                "image_url": "http://localhost:8888/iphone-so/images/13558127351.jpg",
                "highlight": "This is a fantastic offer! You can save 63% with this offer the next time you need an on-site photographer."
            }
        ],
        "21": [
            {
                "id": "21",
                "name": "One Hour Massage",
                "image_url": "http://localhost:8888/iphone-so/images/13558127352.jpg",
                "highlight": "With this special offer receive one hour massage for $35. If you have ever wanted a massage after a long day, this is it! Buy one now for yourself or a loved one. You will save almost 70% with this o"
            }
        ],
        "22": [
            {
                "id": "22",
                "name": "Start your spring cleaning with this Offer! Get one area cleaned for Half-Price!",
                "image_url": "http://localhost:8888/iphone-so/images/13558127353.jpg",
                "highlight": "For only $40 you can save on having your carpet cleaned with this offer! Save 50% with this offer and receive a free gift."
            }
        ],
        "23": [
            {
                "id": "23",
                "name": "Let Their Creativity Unwined",
                "image_url": "http://localhost:8888/iphone-so/images/13558127354.jpg",
                "highlight": "For only $60 children can express themselves with art! With this offer you can see what creativity your child is keeping bottled up with this 2 hour class!"
            }
        ]
    }
}

this is the result of product.json.php with 5 items

{
    "products": {
        "19": [
            {
                "id": "19",
                "name": "Save $240 on your next photo session",
                "image_url": "http://localhost:8888/iphone-so/images/13558151441.jpg",
                "highlight": "This is a fantastic offer! You can save 63% with this offer the next time you need an on-site photographer."
           }
        ],
        "21": [
            {
                "id": "21",
                "name": "One Hour Massage",
                "image_url": "http://localhost:8888/iphone-so/images/13558151442.jpg",
                "highlight": "With this special offer receive one hour massage for $35. If you have ever wanted a massage after a long day, this is it! Buy one now for yourself or a loved one. You will save almost 70% with this o"
            }
        ],
        "22": [
            {
                "id": "22",
                "name": "Start your spring cleaning with this Offer! Get one area cleaned for Half-Price!",
                "image_url": "http://localhost:8888/iphone-so/images/13558151443.jpg",
                "highlight": "For only $40 you can save on having your carpet cleaned with this offer! Save 50% with this offer and receive a free gift."
            }
        ],
        "23": [
            {
                "id": "23",
                "name": "Let Their Creativity Unwined",
                "image_url": "http://localhost:8888/iphone-so/images/13558151444.jpg",
                "highlight": "For only $60 children can express themselves with art! With this offer you can see what creativity your child is keeping bottled up with this 2 hour class!"
            }
        ],
        "24": [
            {
                "id": "24",
                "name": "Custom framing for only $49! An offer valued at $200",
                "image_url": "http://localhost:8888/iphone-so/images/13558151445.jpg",
                "highlight": "Framing doesn’t have to be expensive! Now with this offer you can get $200 worth of framing for only $49. Don’t let your art hang without a frame, take advantage of this offer. "
            }
        ]
    }
}

Upvotes: 0

Views: 1254

Answers (1)

NJones
NJones

Reputation: 27147

From the documentation of NSJSONSerialization's JSONObjectWithData:options:error: method:

The data must be in one of the 5 supported encodings listed in the JSON specification: UTF-8, UTF-16LE, UTF-16BE, UTF-32LE, UTF-32BE.

Since you seem to have control over the server source code, your URL is "http://localhost:8888/", the next bit also applies:

The most efficient encoding to use for parsing is UTF-8, so if you have a choice in encoding the data passed to this method, use UTF-8.

If you truly need characters not in UTF-8 try UTF-16, or encode the special characters in percent escapes.

Upvotes: 1

Related Questions