2015-10-13 125 views
0

我JSON字符串轉換字典

{ 
    abstract = "Today P.M of India Mr. Narender Modi visited for the eve of Agarasen \njayanti 
\n"; 
    created = 1444733102; 
    imgUrl = ""; 
    nid = 12; 
    title = "Latest news"; 
} 

的字符串格式我想轉換爲NSDictionary中

NSError *jsonError; 
NSData *objectData = [[[tableDataArray objectAtIndex:indexPath.row]objectForKey:@"NewsValue"] dataUsingEncoding:NSUTF8StringEncoding]; 
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objectData options:NSJSONReadingMutableContainers error:&jsonError]; 
NSLog(@"%@",json); 

他們給予的反應是空請幫助。

+0

它從哪裏來? web服務 – Jamil

+0

如果我將您的「字符串」複製/粘貼到JSON驗證器中,則不正確。你從哪裏得到它?它似乎已經在NSDictionary格式。 – Larme

+0

檢查jsonError給出的內容,並檢查objectData是否爲空 – Iftikhar

回答

0

這已經在JSON數據的詞典合成。

所以你不需要轉換。只需檢索數據。

NSString * abstract = [Dic valueForKey:@"abstract"]; 
-1

請使用下面碼

的NSData *目標數據= [[[tableDataArray objectAtIndex:indexPath.row] objectForKey:@ 「NewsValue」] 的NSString * strResponce = [[NSString的頁頭]目標數據編碼:NSUTF8StringEncoding ]。

NSMutableDictionary *dictResponse = [NSJSONSerialization JSONObjectWithData:[strResponce dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:&error]; 

如果([dictResponse isKindOfClass:[的NSMutableDictionary類]]){

的NSString * abstractStr = [dictResponse valueForKey:@ 「抽象」];

}