ios - 从 JSON 获取字典

标签 ios json nsdictionary

我正在尝试从 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];
}

谢谢。

最佳答案

请按照下面的代码

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

试试这个..

关于ios - 从 JSON 获取字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18636113/

相关文章:

ios - 应用程序因暂停动画和 NSInternalInconsistencyException 而崩溃

javascript - 如何使用名为 "@attributes"的对象读取 JSON 输出?

objective-c - 从 NSArray 为 UITableView 创建索引

javascript - AngularJS - 使用 ng-repeat 打印 JSON

ios - 从 NSMutableDictionary 中移除对象

ios - 如何在序列化为 json 之前将 NSDictionary 添加到另一个 NSDictionary

ios - 终止应有的内存压力

ios - dispatch_async 用于从 url 获取许多图像

ios - 连接两个 BLE 外设的流程是怎样的?

php - Jquery循环通过json数组获取数据并用逗号分隔?