我正在从服务器获得一个JSON
响应。我正在使用以下代码转换该响应:
NSString *respone = [[NSString alloc]initWithData:receivedData encoding:NSUTF8StringEncoding];
NSLog(@"%@",respone); //null
NSString *string = [[NSString alloc] initWithData: receivedData encoding:NSASCIIStringEncoding];
NSLog(@"%@",string); //getting The String
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:receivedData
options:kNilOptions
error:&error];
NSLog(@"%@",jsonArray ); //null (I have to use this Array not the string )
现在,我的问题是对于小的JSON
响应,它工作得很好,但是在某些情况下,我得到了一个响应(receivedData)
超过5080 bytes
,在这种情况下,我的response
和jsonArray
都会出现null
。请帮帮忙
我的Response Json
是:
{“清单清单”:[{“listeddate”:“06/06/2015”,“可用单位”:1,“照片”:{“}”,{“图像”:“}”,“}”,“可供贸易之用”:真,“类别”is :266,“清单名称”:“Godzilla”( DVD,1998年,封闭字幕)“,”描述“:”这是DVD上关于哥斯拉(1998年)的清单“。光盘从未被观看过。原案中有原创艺术品。运输:我将收取1.05美元的跟踪号码。“、”次分类“、”DVD电影“、”类别名称“、”DVD‘s/ Media“、”子分类名称“:”null“、”父类别“:”DVD’s/ Media“、”子类别“:266、”清单“:86、”子分类“:”null“}、{"listeddate":"06/06/2015”、“units现有”:1、“照片”:{“image”:“}”、"availablefortrade":true,“类别”:53,“清单名称”:“医学手册-外科护理手册,喷水屋,好书”,“说明”:“医疗-外科护理产品类别:书籍ISBN : 1582554455标题:医疗手册-外科护理EAN : 9781582554457作者:喷水屋装订:平装出版商: Lippincott Williams & Wilkins出版日期: 2005-07-28页: 1024签名:假第一版:假粉尘夹克:虚假清单价格(MSRP):46.95英尺高: 1.3000英寸宽: 5.0000英寸长: 8.0000英寸: 1.5500磅:良好的阅读状况,一些脊柱磨损。我们公司致力于在eBay上为您提供最优质、最低成本的产品。我们接受PayPal为所有eBay订单付款。请参阅以下付款详情。此项目可提供多个传送选项。有关更多细节,请参见下面的内容,并选择最方便的托运选项。国际航运:进口关税、税金和费用不包括在项目价格或运费中。这些费用是买方的责任。请与贵国海关核实,以确定这些额外费用将在投标/购买之前。这些费用通常由送货(运输)公司收取,或者当你捡起货物时,不要把它们混同为额外的运费。我们不标记低于价值的商品价值,也不将物品标记为“礼物”。美国和国际政府法规禁止此类行为。回报你的满足感对我们非常重要。在留下负面反馈之前,请通过eBay中可用的方法与我们联系。任何缺陷,损坏,或与您的项目的重大差异,必须报告给我们在7天内收到该项目或30天后,装运日期。退货必须在装运通知后30天内邮戳。未交货必须在装运通知后30天内报告.无法交付、无人认领、被拒绝或地址不足的,将需支付20%的补货费和运费。“TextBooks”、“分类名称”:“图书”、“次分类名称”:“空”、“父类别”:“图书”、“次分类名称”:53、“库存品”:87、“次分类”:“null”}、{"listeddate":"06/06/ 2015“、"unitsavailable":1、”照片“:{”图像“:”}“、”可利用贸易“:真、”类别“:266,“库存名称”:“西翼-完整的第一季( dvd,2003,4-光盘集)”,“描述”:“这次拍卖是为一套DVD盒装电影:DVD上面列出的。使用DVD,但它确实工作的fineâ?¦.no问题或损坏/在原始框中,除非另有说明。我们把多个项目的运输结合在一起。如果该项目以2.99美元的运费列出,它将为每增加一项额外运费1美元。较大的项目组合使用总价格的权重。我们接受贝宝(优先)。如果您有任何疑问,请在营业时间内致电256-894-9994,每周7天:上午8点至下午6点。“、”次类别名称“:”DVD电影“、”类别名称“:”DVD的/媒体“、”次类别名称“:”null“、”父类别“:”DVD/ Media“、”子类别“:266、”库存品“:88、”次分类“:”null“}、{"listeddate":"06/08/2015”、"unitsavailable":1、“图片”:{“图像”:“}”,“可供交易”:真,“类别in”:526,“库存名称”:“大脑年龄:每天训练你的大脑分钟(任天堂DS,2006年)游戏”,“描述”:“拍卖是任天堂DS大脑时代的电子游戏,没有案例或指示,只有游戏。”很好的条件,就像新的一样。请在投标前提出任何和所有的问题,如果你不打算付款,请不要出价。感谢您的查找!、“子类别名称”:“其他”、“分类名称”:“视频游戏”、“子类别名称”:“空”、“父类别”:“视频游戏”、“子类别”:526、“清单”:97、“次分类名称”:“null”}}
发布于 2015-06-08 09:06:04
这是由于您的receivedData
json字符串在json的描述键中包含了一些空间字符。
{
"listeddate": "06/06/2015",
"unitsavailable": 1,
"photos": [
{
"image": ""
}
],
"availablefortrade": true,
"categoryid": 53,
"inventoryname": "Handbook of Medical-Surgical Nursing, Springhouse, Good, Books",
"description": "payment | shipping rates | returns Handbook of Medical-Surgical Nursing Product Category : Books ISBN : 1582554455 Title : Handbook of Medical-Surgical Nursing EAN : 9781582554457 Authors : Springhouse Binding : Paperback Publisher : Lippincott Williams & Wilkins Publication Date : 2005-07-28 Pages : 1024 Signed : False First Edition : False Dust Jacket : False List Price (MSRP) : 46.95 Height : 1.3000 inches Width : 5.0000 inches Length : 8.0000 inches Weight : 1.5500 pounds Condition : Good Great reading copy, some spine wear. NO INTERNATIONAL SHIPPING About Sandusky Bay Area Goodwill Industries Our company is dedicated to providing you with the best quality, lowest cost products on eBay. Payment We accept PayPal for all eBay orders. Please see payment details below. Shipping Multiple shipping options are available for this item. For more detail, please see below, and select the shipping option that is most convenient for you. International Shipping: Import duties, taxes, and charges are NOT INCLUDED in the item price or shipping charges. These charges are the buyer's responsibility. Please check with your country's customs office to determine what these additional costs will be prior to bidding / buying. These charges are normally collected by the delivering freight (shipping) company or when you pick the item up do not confuse them for additional shipping charges. We do not mark merchandise values below value or mark items as \"gifts.\" US and International government regulations prohibit such behavior. Returns Your satisfaction is very important to us. Please contact us via the methods available within eBay regarding any problems before leaving negative feedback. Any defects, damages, or material differences with your item, must be reported to us within 7 days of receipt of the item or 30 days from date of shipment. The returned merchandise must be postmarked within 30 days of the shipment notification. Non-deliveries must be reported within 30 days of Shipment Notification. Undeliverable, unclaimed, refused, or insufficient addresses will be subject to a 20% restocking fee plus the cost of shipping. � 2015 Sandusky Bay Area Goodwill Industries",
"subcategoryname": "TextBooks",
"categoryname": "Books",
"subsubcategoryname": "null",
"parentcategory": "Books",
"subcategoryid": 53,
"inventoryid": 87,
"subsubcategoryid": "null"
}
它没有被NSUTF8StringEncoding
正确地编码,而且在成功地编码数据之前,它正在破坏它并提供“空”字符串。
试着用这种方式做
NSString *string = [[NSString alloc] initWithData:receivedData encoding:NSASCIIStringEncoding];
NSData *utf8Data = [string dataUsingEncoding:NSUTF8StringEncoding];
// ... give utf8Data to NSJSONSerializer
发布于 2015-06-08 11:09:56
您只需要使用NSData转换从服务器接收到的原始NSJSONSerialization。而且,只有在服务器或代码给出部分响应的情况下,才会转换完整的NSData。不能解析部分响应。转换为字符串和返回并不总是适用于有效的JSON。这就是为什么我希望看到生成NSData的代码;这很可能是问题的根源所在。
如果NSJSONSerialization返回零,则检查错误代码。不管错误代码是什么,如果它返回零,那么错误就在您收到的JSON数据中。服务器正在发送错误的数据。您不能试图修复接收端的数据,这是一个需要在服务器上修复的问题。
https://stackoverflow.com/questions/30702590
复制相似问题