0

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. "
            }
        ]
    }
}
4

1 回答 1

1

NSJSONSerialization'JSONObjectWithData:options:error:方法的文档中:

数据必须采用 JSON 规范中列出的 5 种支持的编码之一:UTF-8、UTF-16LE、UTF-16BE、UTF-32LE、UTF-32BE。

由于您似乎可以控制服务器源代码,因此您的 URL 是“ http://localhost:8888/”,下一点也适用:

用于解析的最有效编码是 UTF-8,因此如果您可以选择对传递给此方法的数据进行编码,请使用 UTF-8。

如果您确实需要不在UTF-8tryUTF-16中的字符,或者在百分比转义中编码特殊字符。

于 2012-12-18T20:10:30.430 回答