下面列出的代码返回4个项目非常好但是当我增加项目数量说限制5然后它返回null,有人可以告诉我为什么它的行为是这样的吗?
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返回以下4项
{
"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!"
}
]
}
}
这是product.json.php的结果,包含5个项目
{
"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. "
}
]
}
}
答案 0 :(得分:1)
来自NSJSONSerialization
的{{1}}方法的文档:
数据必须位于中列出的5种受支持的编码之一中 JSON规范:UTF-8,UTF-16LE,UTF-16BE,UTF-32LE,UTF-32BE。
由于您似乎可以控制服务器源代码,因此您的网址为“JSONObjectWithData:options:error:
”,下一位也适用:
用于解析的最有效编码是UTF-8,所以如果你 可以选择对传递给此方法的数据进行编码,使用UTF-8。
如果您确实需要不在http://localhost:8888/
尝试UTF-8
的字符,或者在百分比转义中对特殊字符进行编码。