2013-09-05 149 views
0

我想获得字典表单JSON,但下面的代码剂量似乎工作。我得到了JSON,但无法从中得到字典。从JSON获取字典

NSString *str = [[NSMutableString alloc] initWithData:responseCust encoding:NSUTF8StringEncoding]; 
NSLog(@"CUSTOMER string -----################ %@", str); 

if(str.length>5) 
{ 
    SBJSON *jsonparser=[[SBJSON alloc]init]; 
    NSDictionary *res= [jsonparser objectWithString:str]; 

    NSLog(@"Contants Results %@",res); 

    [jsonparser release]; 
    [str release]; 
} 

谢谢。

+4

使用NSJSONSerialization。 –

+0

请更好地解释你自己。你会得到一个(显然是有效的)JSON字符串,但“Contants Results”打印出“(null)”?如果是这样,请先将JSON复制/粘贴到联机JSON解析器中以检查其有效性。 –

+0

阅读https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html –

回答

2

请按照下面的代码

NSURL * url=[NSURL URLWithString:str]; 

NSData * data=[NSData dataWithContentsOfURL:url]; 

NSError * error; 

//Get json data in Dictionary 
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers error: &error]; 

NSLog(@"%@",json); 

试试这个..

+0

嗨,用你的方法,现在我得到这个错误-JSONValue失败。错误跟踪是:( “错误域= org.brautaset.JSON.ErrorDomain代码= 3”无法识别的前导字符“UserInfo = 0x737cdf0 {NSLocalizedDescription =无法识别的前导字符}” ) 但json看起来很干净。 – LeXeR

+0

你检查你的网址是否正确,它会从服务器获取数据检查它.. – Jitendra

+0

是的,该网址是正确的。这个错误显示在我的控制台中。 – LeXeR

0

刚刚尝试要么

NSDictionary *dict = [str JSONValue]; 
NSLog(@"%@", dict); 

OR

NSError *error; 
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseCust options:nil error:&error]; 
NSLog(@"%@", dict); 
+0

尝试了所有上述方法,似乎没有任何工作。 JSON也是正确的,检查它在json.parser.online.fr ..... – LeXeR

+0

真的很奇怪,你可以粘贴JSON的问题? – Emilio

1

使用NSJSONSerialization和利用

+ (id)JSONObjectWithData:(NSData *)data options:(NSJSONReadingOptions)opt error:(NSError **)error to convert JSON to foundation object. 

希望这有助于

0
+(NSDictionary *)converJsonStringToDictionary:(NSString *)jsonString 
{ 
    NSString *stringToConvert = jsonString; 

    if (![self isObjectEmpty:stringToConvert]) { 

     NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; 

     NSError *error = nil; 
     NSDictionary *convertedData = nil; 

     convertedData = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; 

     if (error != nil){ 
      DLog(@"Error converting jsonString to dictionary: %@", [error description]); 
      convertedData = nil; 
     } 
     else if ([convertedData isKindOfClass:[NSDictionary class]]) { 
      DLog(@"The converted data is of kind NSDictionary"); 
     } 
     else if ([convertedData isKindOfClass:[NSArray class]]){ 
      DLog(@"The converted data is of kind NSArray"); 
      convertedData = nil; 
     } 
     else{ 
      DLog(@"The converted data is not NSDictionary/NSArray"); 
      convertedData = nil; 
     } 
     return convertedData; 
    } 
    else{ 
     DLog(@"The received jsonString is nil") 
     return nil; 
    } 
} 
0

这里,尝试添加编码...

NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error]; 

NSLog(@"dict: %@", jsonDict); 

入住这里some samples on how to handle json on objective-c