2012-12-18 55 views
0

下面列出的代碼返回4個項目非常好,但是當我增加項目數量說限制5然後它返回null,有人可以告訴我爲什麼它的行爲是這樣嗎?NSJSONSerialization返回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

我們需要更多信息。 '錯誤'有什麼有趣的地方嗎? 'product.json.php'返回的是什麼,返回4和5對象之間的JSON有什麼區別? – gregheo

+0

向我們展示返回5個產品的JSON字符串?返回值NULL表明它不能被解析/序列化,即它可能不是有效的JSON。 – ophychius

+0

我編輯了這個問題,你可以看到product.json.php的返回,它返回了4個項目非常好,但是當我想獲取5個產品時,它返回null – MIrfan

回答

1

NSJSONSerialization的文檔的結果的JSONObjectWithData:options:error:方法:

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.

既然你似乎已經在服務器的源代碼控制,您的網址是「http://localhost:8888/」,下位也同樣適用:

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.

如果你真正需要的字符不UTF-8嘗試UTF-16,或編碼特殊字符百分比逃逸。