Not all keys work when converting JSON String to NSDictionary

感情迁移 提交于 2020-01-04 07:00:32

问题


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

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)

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.

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.


回答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.

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.



来源:https://stackoverflow.com/questions/28506887/not-all-keys-work-when-converting-json-string-to-nsdictionary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!