ios - 在 DetailViewController 中将 JSON 字符串转换为 NSDictionary

标签 ios json uitableview nsdictionary nsdata

试图找出为什么从 tableviewcontroller 到Detailviewcontroller 的 json segue 不起作用。做了 NSLog 来查看数据是否通过,这就是 dvc 中显示的内容。

"data.detail" NSLog:

(
        {
        emails =         {
            10 = j;
            11 = k;
            12 = l;
            9 = i;
        };
        links =         {
            1 = a;
            2 = b;
            3 = c;
            4 = d;
        };
        location =         {
            13 = m;
            14 = n;
            15 = o;
            16 = p;
        };
        numbers =         {
            5 = e;
            6 = f;
            7 = g;
            8 = h;
        };
    }
)

然后我...

尝试将json数据转换为detailviewcontroller中的tableview。

NSString *rawString = [NSString stringWithFormat:@"%@", data.detail];
NSString *jsonString = rawString;
NSData *JSONdata = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError *jsonError = nil;

但是将 JSON 转换为 NSData 后,NSDictionary 只会在 NSLog 中输出“null”。

NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:JSONdata options:0 error:&jsonError];
NSArray *items = [dic valueForKeyPath:@"email"];
NSLog(@"dic %@", dic);
NSLog(@"items %@", items);

帮助!非常感谢。

最佳答案

更改以下代码行

NSArray *items = [dic valueForKeyPath:@"email"];

NSArray *items = [dic valueForKeyPath:@"emails"];

如果你仍然得到 nslog nil 而不是改变

NSString *rawString = [NSString stringWithFormat:@"%@", data.detail];

喜欢这个

NSString *rawString = @"{\"emails\" :{\"10\" : \"j\",\"11\" : \"k\",\"12\" : \"l\",\"9\" : \"i\"},\"links\" :{\"1\" : \"a\",\"2\" : \"b\",\"3\" : \"c\",\"4\" : \"d\"}}";

关于ios - 在 DetailViewController 中将 JSON 字符串转换为 NSDictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27226917/

相关文章:

ios - 如何在 Iphone 6 上扫描不公布名称的 BLE 设备

ios - 您可以使用 MonoTouch 使用 Kerberos 身份验证连接到 ASP.net soap 服务吗?

ios - 如何从另一个 UIViewController 启动 SLComposeServiceViewController

ios - 如何在 UITableViewRowAction 中添加图像?

ios - 使用 NSMutableArray 中的 BOOL 对象来确定单元格状态

ios - 双segue tableView?

ios - NSClassFromString 在 OC 中运行良好,但在 Swift 中崩溃

php - Laravel JSON 从 POST 读取

json - Alamofire POST JSON 数组

javascript - 为什么我使用 JSON P api 会收到 404 Forbidden 错误?