我正在尝试获取first key
"A"
objective C
值而未提及使用NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves | NSJSONReadingMutableContainers error:&error];
NSDictionary *response = [JSON[@"response"]firstObject];
response = {
A = {
company = (
{
no = "115";
student = "Mich";
school = (
{
grade = A;
}
);
test = "<null>";
office = tx;
}
);
};
}
的{{1}}的关键名称。我无法准确到达,请帮助我从下面回复。
val authWithHeader = headerValueByName("Access_Token")
.flatMap(accessToken => authenticate(validate(accessToken)))
val authenticatedJsonRoute = cors &
authWithHeader &
headerValueByName("Tenant_Name") &
respondWithMediaType(MediaTypes.`application/json`)
override val getLayer =
(get & path("api" / Segment / "model" / "layers")) { modeledSystemName =>
authenticatedJsonRoute { (authInfo, tenantName) =>
onComplete(service.getlayers(modeledSystemName, tenantName)) {
case Success(layers) => complete(layers)
//case Success(None) => complete(NotFound, "Release version not found")
case Failure(ex) => complete(InternalServerError, s"An error occurred: ${ex.getMessage}")
}
}
}
答案 0 :(得分:0)
根据您的具体要求,有几种方法可以做到这一点。如果您只需要访问JSON[@"response"]
中每个密钥的值,则可以枚举JSON
字典:
[JSON enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
NSDictionary *dict = (NSDictionary *)obj;
...
if (shouldStop) { // whatever condition you want, if any
*stop = YES;
}
}];
如果您需要某种订购,则需要使用[JSON allKeys]
:
NSArray *keys = [[JSON allKeys] sortedArrayUsing...]; // Use whichever sort method you like
for (NSString *key in keys) {
NSDictionary *dict = JSON[key];
...
}
如果您想要的只是值,则可以使用[JSON allValues]
。如果需要排序。