[英]Parsing a JSON value in Objective-C with variable ID/key
I have searched this extensively, but so far I could not find a question that matches my problem. 我已经对此进行了广泛的搜索,但是到目前为止,我找不到与我的问题匹配的问题。 I have a 'unique' JSON output and trying to parse it in Objective-C.
我有一个“独特的” JSON输出,并尝试在Objective-C中解析它。
First of all, this is the JSON that I must parse: 首先,这是我必须解析的JSON:
{
"code": 1,
"req": {
"123": [ //this can be different all the time
{
"item_id": "44",
"item_value": "the value",
"item_code": "21z"
},
{
"item_id": "45",
"item_value": "another value",
"item_code": "l30"
}
]
}
}
As can be seen above, the "123" right under "req" can vary, so I can't hardcode the value there in my Objective-C. 从上面可以看到,“ req”下面的“ 123”可以变化,因此我无法在我的Objective-C中将值硬编码。
So far i have been trying to use NSMutableDictionary to receive the HTTP response: 到目前为止,我一直在尝试使用NSMutableDictionary来接收HTTP响应:
NSMutableDictionary dict = [NSJSONserializationWithData:responseData options:options error:&error];
NSArray *array = [[dict objectForKey@"req"];
But from here I don't know how to specify the variable (123) key.. because it can differ everytime. 但是从这里开始,我不知道如何指定变量(123)键。因为它每次都可能不同。
Is there going to be multiple arrays in the "req" object? “ req”对象中是否会有多个数组?
If not you could loop through the objects inside the "req" object checking if the object's type matches an NSArray 如果不是,则可以遍历“ req”对象内的对象,检查对象的类型是否与NSArray匹配
eg 例如
for(id obj in array)
{
if([obj isKindOfClass:[NSArray class]])
{
//123 object = obj
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.