在将JSON字符串转换为NSDictionary时,并非所有键都有效

时间:2022-08-30 14:10:15

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

我使用下面的代码将JSON字符串文本转换为为每个项目持有一个NSDictionary的数组:

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

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:

在从我的测试设备上移除应用程序后,当尝试访问时,应用程序开始崩溃,在一个for循环值[i][@"name"]中。当查看检查器中的值时,我看到以下内容:

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)

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

进一步展开时,应该是名称和版本的键都显示为类型(__NSCFConstantString *)

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

在将应用程序从设备中移除之前,该操作就已经开始工作了,并且没有对代码的这一部分做任何更改。

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

对于可能导致这种情况的原因,或者将json字符串转换为字典的更好方法,您有什么想法吗?

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

更新:我把“名字”和“版本”改成了“游戏”和“gersion”,效果非常好。

1 个解决方案

#1


1  

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.

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

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.

我们仍然不确定是什么原因导致设备进入这个状态,因为运行“干净的构建文件夹”几次,关闭和重新启动XCode,重新启动iPad没有帮助我们。

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.

我考虑过把这个问题去掉,但是因为我们不知道是什么原因导致了这个问题,我想其他人将来可能会遇到这个问题,这个答案可能会对他们有所帮助。如果有人能对这种情况的发生提供一些见解,我将欣然接受你的回答。

#1


1  

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.

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

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.

我们仍然不确定是什么原因导致设备进入这个状态,因为运行“干净的构建文件夹”几次,关闭和重新启动XCode,重新启动iPad没有帮助我们。

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.

我考虑过把这个问题去掉,但是因为我们不知道是什么原因导致了这个问题,我想其他人将来可能会遇到这个问题,这个答案可能会对他们有所帮助。如果有人能对这种情况的发生提供一些见解,我将欣然接受你的回答。