2015-06-08 20 views
1

我得到一個JSON來自服務器的響應。JSON未得到轉換爲NSString

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響應它工作正常,但在某些情況下,我得到了在這種情況下,AA響應(receivedData)超過5080 bytesresponsejsonArray都來了:我使用下面的代碼轉換是響應null。請幫助

Response Json是:

{ 「inventorylist」:[{ 「listeddate」: 「2015年6月6日」, 「unitsavailable」:1, 「照片」:[{ 「形象」 :「」},{「image」:「」}],「availablefortrade」:true,「categoryid」:266,「inventoryname」:「Godzilla(DVD,1998,Closed Caption)」,「description」在DVD上的Godzilla(1998)上市,Disc從來沒有被觀看過,原創作品帶有原創藝術作品出貨:我會收取1.05美元作爲追蹤號碼,越早付款給我,越早到達您的信箱。 「子類別名稱」:「DVD電影」,「類別名稱」:「DVD /媒體」,「子類別名稱」:「null」,「父類別」:「DVD /媒體」,「子類別」:266,「inventoryid」 「subsubcategoryid」: 「空」},{「listeddate 「:」06/06/2015「,」unitsavailable「:1,」photos「:[{」image「:」「}],」availablefortrade「:true,」categoryid「:53,」inventoryname「:」Handbook of醫療外科護理,溫室,好,書籍「,」描述「:」付款|運費|返回醫療外科護理手冊產品類別:書籍ISBN:1582554455標題:醫療外科護理手冊EAN:9781582554457作者:Springhouse Binding:平裝出版商:利平科特威廉姆斯& Wilkins出版日期:2005-07-28頁數:1024簽名:假第一版:假灰塵外套:假價格表(MSRP):46.95高度:1.3000英寸寬度:5.0000英寸長度:8.0000英寸重量:1.5500磅條件:良好極好的閱讀副本,一些脊柱磨損。沒有國際航運關於桑達斯基灣地區Goodwill Industries我們公司致力於爲您提供eBay上最優質,最低成本的產品。付款我們接受所有eBay訂單的PayPal。請參閱下面的付款細節。運送此項目有多種運送選項可供選擇。有關更多詳細信息,請參閱下面的內容,並選擇最適合您的送貨選項。國際運費:進口關稅,稅款和費用不包括在物品價格或運費中。這些費用是買方的責任。請與您所在國家的海關辦事處覈實,以確定這些額外費用將在投標/購買之前進行。這些費用通常由交付貨運(運輸)公司收取,或者當您選擇物品時不要混淆它們以獲得額外的運輸費用。我們不會將商品價值低於價值或將商品標記爲「禮品」。美國和國際政府規定禁止此類行爲。退貨您的滿意對我們非常重要。請在離開負面反饋之前,通過eBay內有關任何問題的方法與我們聯繫。任何與您的物品有關的缺陷,損壞或重大差異,必須在收到物品的7天內或從裝運日起30天內向我們報告。退回的商品必須在發貨通知後30天內寄出。未交貨必須在裝運通知後30天內報告。無法送達,無人認領,拒絕或地址不足,將收取20%的補貨費加上運費。 2015年桑達斯基灣地區商譽行業「,」子類別名稱「:」文本書籍「,」類別名稱「:」書籍「,」子類別名稱「:」空白「,」父類別「:」書籍「,」子類別「 inventoryid 「:87,」 subsubcategoryid 「:」 空 「},{」 listeddate 「:」 2015" 年6月6日, 「unitsavailable」:1, 「照片」:[{ 「圖像」: 「」}],「availablefortrade 「:true,」categoryid「:266,」inventoryname「:」The West Wing - The Complete First Season(DVD,2003,4-Disc Set)「,」description「:」這次拍賣是爲1 DVD盒子集電影:上面列出的DVD。 DVD使用,但它確實工作正常?|。沒有問題或損壞/在原來的盒子來,除非另有說明。我們確實結合了多件商品的運費。如果該商品以2.99美元的運費上市,它將會爲每件額外商品運送1美元的額外運費。使用整個價格的重量合併大項目。我們接受貝寶(首選)。如果您有任何疑問或疑問,請在營業時間內致電256-894-9994:每週7天,每天上午8點至下午6點。「,」subcategoryname「:」DVD Movies「,」categoryname「:」DVD's/Media「,」subsubcategoryname 「:」null「,」parentcategory「:」DVD's/Media「,」subcategoryid「:266,」inventoryid「:88,」subsubcategoryid「:」null「},{」listeddate「:」06/08/2015「, 「unitsavailable」:1,「photos」:[{「image」:「」}],「availablefortrade」:true,「categoryid」:526,「inventoryname」:「Brain Age:每天訓練你的大腦(任天堂DS,2006)遊戲只有「,」描述「:」拍賣是Nintendo DS腦時代視頻遊戲,沒有案例或說明,只是遊戲。非常好的條件,幾乎像新的一樣。請在投標前提出任何和所有問題,如果您不打算付款,請不要出價。 「,」子類別名稱「:」其他「,」類別名稱「:」視頻遊戲「,」subsubcategoryname「:」null「,」parentcategory「:」視頻遊戲「,」子類別「:526,」inventoryid「: 97, 「subsubcategoryid」: 「空」}]}

+0

嘗試發佈您的錯誤'JSONObjectWithData' – Leo

+0

請檢查 - http://stackoverflow.com/questions/7520615/how-to-convert-an-nsdata-into-an-nsstring-hex-string/12442672 #12442672 –

+0

如果你嘗試''記錄'receivedData'的內容會怎麼樣? – mttrb

回答

2

這是因爲,你的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 
+0

非常感謝。它解決了我的錯誤。只是一個問題。我可以在任何地方添加此代碼?我的意思是,如果我沒有得到任何特殊的字符,並有'NSUTF8StringEncoding' ony仍然可以添加此? – Shruti

+0

@Shruti你可以添加,但它不是正確的解決方案,它是一種補丁。必須在Web端點上正確地使用它,以免再次發生。正確的解決方案是確保Web端點使用的是哪種類型的編碼,並且您還必須使用相同的編碼類型。 –

+0

其實我問過他們。他們說我們沒有使用任何編碼。他們正在使用一些tool.library來獲取json。 – Shruti

-1

這可能有助於你...檢查JSON和使用下面的代碼...

id object = [NSJSONSerialization 
       JSONObjectWithData:recievedData 
       options:kNilOptions 
       error:&error]; 

if (error) 
    { 
     NSLog(@"Error in data:%@",[error description]); 
    } 
    if([object isKindOfClass:[NSString class]] == YES) 
    { 
     NSLog(@"String from server"); 
    } 
    else if ([object isKindOfClass:[NSDictionary class]] == YES) 
    { 
     NSLog(@"Dictionary from server"); 
    } 
    else if ([object isKindOfClass:[NSArray class]] == YES) 
    { 
     NSLog(@"Array from server"); 
    } 

NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:recievedData options:NSJSONReadingAllowFragments error:&error]; 
NSLog(@"Response Dic %@",responseDic); 
+0

但是,如果Json很小,我的代碼工作正常,但如果Json很大,那麼它就是null。 – Shruti

+0

更改選項'NSJSONReadingAllowFragments'並讓我知道... –

+0

我做了同樣的迴應,即null – Shruti

2

你永遠只能轉換您從使用NSJSONSerialization服務器接收到的原始的NSData。如果服務器或您的代碼給出了部分響應,那麼您只能將完成 NSData轉換。部分響應無法解析。轉換爲字符串並返回並不總是與有效的JSON一起工作。這就是爲什麼我想看到生成你的NSData的代碼;這很可能是問題來自何處。

如果NSJSONSerialization返回nil,則檢查錯誤代碼。無論錯誤代碼是什麼,如果返回nil,那麼錯誤出現在您收到的JSON數據中。服務器正在向您發送錯誤數據。您不得嘗試修復接收端的數據,這是一個需要在服務器上修復的問題。

+0

謝謝你的解釋。它給了我'無法將數據轉換爲字符串周圍的字符3647'錯誤。這是一個特殊的角色。我要求後端的人來處理這個問題,但我想修復它在我身邊。它有什麼好的解決方案? – Shruti