Ios 从JSON获取字典

Ios 从JSON获取字典,ios,json,nsdictionary,Ios,Json,Nsdictionary,我试图获取字典形式的json,但下面的代码似乎不起作用。我正在获取JSON,但无法从中获取字典 NSString *str = [[NSMutableString alloc] initWithData:responseCust encoding:NSUTF8StringEncoding]; NSLog(@"CUSTOMER string -----################ %@", str); if(str.length>5) { SBJSON *jsonparse

我试图获取字典形式的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];
}

谢谢。

试着或者

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

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

只要试着或者

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

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

+ (id)JSONObjectWithData:(NSData *)data options:(NSJSONReadingOptions)opt error:(NSError **)error to convert JSON to foundation object.
希望这有助于使用和利用

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

希望这有帮助,请按照下面的代码操作

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);

试试这个..

请按照下面的代码操作

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);
+(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;
    }
}

试试这个..

在这里,尝试添加编码

+(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;
    }
}
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error];

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

请检查此处,查看是否存在编码。请尝试添加编码

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

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

检查此处是否使用NSJSONSerialization。请更好地解释您自己。您会得到一个(显然有效的)JSON字符串,但“Contants Results”会打印(null)?如果是这样,首先将您的JSON复制/粘贴到在线JSON解析器中以检查其有效性。阅读@Nitin检查我的代码。使用NSJSONSerialization。请更好地解释您自己。您会得到一个(显然有效的)JSON字符串,但“Contants Results”会打印(null)?如果是这样,首先将您的JSON复制/粘贴到在线JSON解析器中以检查其有效性。JSON也是正确的,在JSON.parser.online.fr上检查过……真奇怪,你能把JSON粘贴到这个问题上吗?尝试了上述所有方法,似乎都不起作用。JSON也是正确的,在JSON.parser.online.fr上检查了它……真奇怪,你能在问题上粘贴JSON吗?嗨,使用了你的方法,现在我得到了这个错误-JSONValue失败。错误跟踪为:(“Error Domain=org.brautaset.JSON.ErrorDomain Code=3\“Unrecogned leading character\”UserInfo=0x737cdf0{NSLocalizedDescription=Unrecogned leading character}”),但JSON看起来很干净。是的,url是正确的。此错误将显示在我的控制台中。遵循这个链接,这不是因为换行符。我真的对此抱有希望。这是某种看不见的性格。我看不到它,但我知道它在那里。嗨,使用了你的方法,现在我得到了这个错误-JSONValue失败。错误跟踪为:(“Error Domain=org.brautaset.JSON.ErrorDomain Code=3\“Unrecogned leading character\”UserInfo=0x737cdf0{NSLocalizedDescription=Unrecogned leading character}”),但JSON看起来很干净。是的,url是正确的。此错误将显示在我的控制台中。遵循这个链接,这不是因为换行符。我真的对此抱有希望。这是某种看不见的性格。我看不见它,但我知道它在那里。