将JSON字符串转换为NSDictionary时,并非所有键都起作用 [英] Not all keys work when converting JSON String to NSDictionary

查看:131
本文介绍了将JSON字符串转换为NSDictionary时,并非所有键都起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码将JSON字符串文字转换为包含每个项目的NSDictionary的数组:

I'm using the following code to convert a JSON string literal to an array holding an NSDictionary for each item:

NSString* json = @"[{\"name\":\"Item 1\",\"id\":\"999\",\"lang\":\"en\",\"type\":\"A\",\"version\":15}]";
NSData* data = [json dataUsingEncoding:NSUTF8StringEncoding];
NSArray* values = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

从我的测试设备中删除该应用程序后,该应用程序尝试在for循环values[i][@"name"]中访问时开始崩溃.在检查器中查看values时,我看到以下内容:

After removing the app from my test device, the app started crashing when attempting to access, in a for loop values[i][@"name"]. When viewing values in the inspector, I see the following:

values = (NSArray *) @"1 object"
    [0] = (__NSDictionaryM *) 5 key/value pairs
        [0] = (no summary) : @"Item 1"
        [1] = @"id" : @"999"
        [2] = @"type" : @"A"
        [3] = (no summary) : (long)15
        [4] = @"lang" : (no summary)

进一步扩展时,应分别为nameversion的键都显示为类型(__NSCFConstantString *)

When expanded further, the keys that should be name and version are both shown to be the type (__NSCFConstantString *)

这是在从设备中删除应用程序之前的工作,并且未对代码的这一部分进行任何更改.

This was working prior to removing the app from the device, and no changes have been made to this section of the code.

关于什么可能导致这种情况的任何想法,或者将json字符串转换为字典的更好方法?

Any ideas on what could be causing this, or better ways to convert the json string to a dictionary?

更新:我将名称"和版本"分别更改为游戏"和融合",并且运行良好.

UPDATE: I changed "name" and "version" to "game" and "gersion", and it worked perfectly.

推荐答案

尝试调试了几个小时后,我的主管和我决定重新启动正在开发的MacBook.这样就解决了运行时问题,问题中的代码再次按预期工作.

After attempting to debug this for several hours, my supervisor and I decided to restart the MacBook I'm developing on. This resolved the runtime issue, and the code in my question is once again working as expected.

我们仍然不确定是什么原因导致设备进入此状态,因为已多次运行清理构建文件夹",关闭并重新启动XCode以及重新启动iPad并没有任何帮助.

We are still unsure what caused the device to get into this state, since running "Clean Build Folder" several times, closing and relaunching XCode, and restarting the iPad did nothing to help us.

我考虑删除此问题,但是由于我们不知道是什么原因造成的,因此我认为将来可能会有其他人遇到此问题,并且此答案可能会对他们有所帮助.如果有人能提供一些可能导致这种情况的信息,我将很高兴接受您的回答.

I considered removing the question, but since we don't know what caused this I figured someone else may run into this issue in the future and this answer could help them. If anyone could provide some insight into what may have brought this situation about, I will gladly accept your answer.

这篇关于将JSON字符串转换为NSDictionary时,并非所有键都起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆