Obj-C获取JSON密钥名称 [英] Obj-C get JSON key name
问题描述
我正在尝试在PHP和iOS之间创建链接,并且我正在使用JSON.尽管我的JSON数组很奇怪,但我不是创建者,因为它在封闭的API中,所以我无法更改它.
Hi I am trying to create a link between PHP and iOS, and I am using JSON. Although my JSON array is strange, I am not the creator, as this is in a closed API, I cannot change it.
JSON如下:
{"success":true,"errors":[],"data":{"Servers":{"6":"Name here"}}}
6是服务器的ID,另一个是名称.
The 6 is the ID of the server, and the other one is the name.
如您所见,键是ID,值是名称,但是如何在Objective-C中获得ID?我知道如何接收服务器名称,但是要做到这一点,我需要找到服务器ID.
As you can see the key is the ID and the value is the name, but how do I get the ID in Objective-C? I know how to receive the server name, but to do that I need to find the server ID.
这是我当前的代码:
NSDictionary *json = [[NSDictionary alloc] init];
NSError *error;
json = [NSJSONSerialization JSONObjectWithData:_responseData options:kNilOptions error:&error];
NSDictionary *test = [dataDict objectForKey:@"Servers"]
NSLog(@"%@", [test objectForKey:@"6"]); // Server Name (ID of server is 6)
_responseData
仅仅是从PHP脚本接收到的JSON数据.
_responseData
is just the JSON data received from the PHP script.
推荐答案
您将执行以下操作:
[test enumerateKeysAndObjectsUsingBlock:^(NSString* serverId, NSString *serverName, BOOL *stop) {
// Process data here.
}];
这篇关于Obj-C获取JSON密钥名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!