我从服务器收到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,在这种情况下,我的responsejsonArray都即将成为null。请帮忙

我的Response Json是:

{“库存清单”:[{“上市日期”:“06/06/2015”,“可用单位”:1,“照片”:[{“图像”:“”},{“图像”:“”}],“availablefortrade“:true,” categoryid“:266,” inventoryname“:” Godzilla(DVD,1998,隐藏式字幕)“,” description“:”这是DVD上Godzilla(1998)的列表。从未观看过此光盘。附带:带有原始艺术品的原包装。运输:我将为跟踪号码收取1.05美元。您越早付款给我,它越早到达您的邮箱。“,” subcategoryname“:” DVD Movies“,” categoryname“:” DVD's / Media”,“subsubcategoryname”:“null”,“parentcategory”:“DVD's / Media”,“subcategoryid”:266,“inventoryid”:86,“subsubcategoryid”:“null”},{“listeddate”:“2015年6月6日“,”单位可用“:1,”照片“:[{”图像“:”“}]],”可用贸易“:true,”类别ID“:53,”库存名称“:”医疗外科手册护理,Springhouse,商品,书籍”,“描述”:“付款|运费|退货”外科护理手册产品类别:书籍ISBN:1582554455标题:外科手术手册护理EAN:9781582554457作者:Springhouse装订:平装出版商:Lippincott Williams&Wilkins出版日期:2005-07-28页数:1024签名:False第一版:False防尘外套:False List Price(MSRP):46.95高度:1.3000英寸宽度:5.0000英寸长度:8.0000英寸重量:1.5500磅条件:良好很好的阅读副本,有些背脊。没有国际运输关于桑达斯基湾地区善意工业我们公司致力于为您提供eBay上质量最好,成本最低的产品。付款我们接受所有eBay订单的PayPal。请在下面查看付款明细。运送此物品有多种运送选项。有关更多详细信息,请参见下文,然后选择最适合您的运输方式。国际运输:商品价格或运输费用中不包含进口关税,税金和费用。这些费用是买方的责任。请先咨询您所在国家/地区的海关,以确定在竞标/购买之前这些额外费用是多少。这些费用通常由送货的货运(运输)公司收取,或者当您提货时,请不要混淆它们的额外运费。我们不会将商品价值标记为低于价值或将商品标记为“礼物”。美国和国际政府法规禁止此类行为。回报您的满意对我们非常重要。在留下负面反馈之前,请通过eBay内提供的有关任何问题的方法与我们联系。与您的物品有关的任何缺陷,损坏或重大差异,必须在收到物品后的7天内或自发货之日起30天内报告给我们。退货商品必须在发货通知后的30天内加盖邮戳。未送达的商品必须在发货通知后的30天内报告。无法送达,无人认领,拒绝或地址不足的情况,将加收20%的进货费加运费。 2015年桑达斯基湾地区商誉工业”,“子类别名称”:“教科书”,“类别名称”:“书籍”,“子子类别名称”:“null”,“父类别”:“书籍”,“子类别”:53,“ventoryid“:87,” subsubcategoryid“:” null“},{” listeddate“:” 06/06/2015“,” unitsavailable“:1,” photos“:[{” image“:”“}],” availablefortrade “:true,” categoryid“:266,”库存名称“:”西翼-完整的第一季(DVD,2003,4碟套装)“,”描述“:”本次拍卖是针对1台DVD盒装电影:上面列出的DVD。使用了DVD,但是可以正常工作。没有任何其他问题或损坏/未在原包装盒中出现,除非另外说明。我们确实将多件物品的运输合并在一起。如果该物品的运费为2.99美元,则每增加一件物品,运费将增加1美元。使用总价格的权重合并较大的项目。我们接受贝宝(首选)。如果您有任何疑问或疑虑,请在工作时间:每周7天,上午8点至下午6点致电256-894-9994。“,”子类别名称“:” DVD电影“,”类别名称“:” DVD的/媒体“,”子类别名称“:” null“,” parentcategory“:” DVD /媒体“,” subcategoryid“:266,” inventoryid“:88,” subsubcategoryid“:” null“},{” listeddate“:” 06/08/2015“, “单位可用”:1,“照片”:[{“图像”:“”}],“可用贸易”:true,“类别类别”:526,“库存名称”:“大脑年龄:每天几分钟训练您的大脑(任天堂) DS,2006)“仅限游戏”,“说明”:“要拍卖的是Nintendo DS Brain Age视频游戏,没有案例或说明,仅是游戏。条件非常好,几乎像新的一样。请在出价之前询问所有问题,如果您不打算付款,请不要出价。感谢您的关注!“,” subcategoryname“:” Other“,” categoryname“:” Video Games“,” subsubcategoryname“:” null“,” parentcategory“:” Video Games“,” subcategoryid“:526,” inventoryid“: 97,“subsubcategoryid”:“null”}]}

最佳答案

这是因为,您的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

关于ios - JSON未转换为NSString,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30702590/

10-11 06:07