JSON响应中NSDictionary内的对象数组-目标c
Posted
技术标签:
【中文标题】JSON响应中NSDictionary内的对象数组-目标c【英文标题】:array of objects inside NSDictionary in JSON response - objective c 【发布时间】:2016-02-03 08:25:44 【问题描述】:我正在尝试从NSDictionary
的 JSON 响应中获取对象列表,然后将其存储在 NSMutableArray
中,但它一直只给我数组中的一个对象,请您帮我提供示例代码,
这是 json 响应:
"errorCd": "00",
"desc": "Success",
"ref": 83,
"statusCode": "1",
"extraData": [
"key": "bal",
"value": "80"
,
"key": "txs",
"value": "[\"id\":2268099999,\"amnt\":100.0,\"curr\":\"JOD\",\"sender\":\"PSPCI\",\"receiver\":\"00962799999992\",\"date\":\"Feb 1, 2016 4:03:25 PM\",\"status\":1,\"type\":5,\"fees\":0.0,\"reference\":40,\"id\":2357099999,\"amnt\":20.0,\"curr\":\"JOD\",\"sender\":\"00962799999992\",\"receiver\":\"PSPCI\",\"date\":\"Feb 2, 2016 12:52:35 PM\",\"status\":1,\"type\":6,\"fees\":0.0,\"reference\":68]"
]
我正在尝试获取键 txs 的值中的列表。
这就是我想要做的:
-(NSDictionary *)getListOfExtraData:(NSMutableArray *)extras
NSDictionary *array = [[NSDictionary alloc] init];
for (NSDictionary *dictionary in extras)
if([[dictionary valueForKey:@"key"] isEqualToString:@"txs"])
array = [dictionary objectForKey:@"value"];
if (array != nil && array.count > 0)
return array;
return nil;
【问题讨论】:
根据响应,该值看起来像是一个字符串,而不是一个数组。我记得它必须是这样的 ["A", "B"] 不带引号。 【参考方案1】:JSON 数据无效,因为value
在"
中有一个对象
你应该像这样发送你的 JSON
"errorCd": "00",
"desc": "Success",
"ref": 83,
"statusCode": "1",
"extraData": [
"key": "bal",
"value": "80"
,
"key": "txs",
"value": [
"id": 2268099999,
"amnt": 100.0,
"curr": "JOD",
"sender": "PSPCI",
"receiver": "00962799999992",
"date": "Feb 1, 2016 4:03:25 PM",
"status": 1,
"type": 5,
"fees": 0.0,
"reference": 40
,
"id": 2357099999,
"amnt": 20.0,
"curr": "JOD",
"sender": "00962799999992",
"receiver": "PSPCI",
"date": "Feb 2, 2016 12:52:35 PM",
"status": 1,
"type": 6,
"fees": 0.0,
"reference": 68
]
]
【讨论】:
如果 OP 无法控制 JSON 编码怎么办? json 对象响应一般包含任何键值对象,并利用值放置我们需要的任何值对象或字符串或数组【参考方案2】:好吧,我会说 JSON 是不寻常的;看起来它可以是字符串化的数字 ("80"
) 或 JSON 数组。
你需要进一步解析内部数组,用:
NSData *jsonResponse = ...; // Response from server
NSError *error = nil;
NSDictionary *topLevelDict = [NSJSONSerialization JSONObjectWithData:jsonResponse
options:0
error:&error];
if (topLevelDict)
NSArray *extraData = topLevelDict[@"extraData"];
for (NSDictionary *innerDict in extraData)
if ([innerDict[@"key"] isEqualToString:@"txs"])
NSString *valueStr = innerDict[@"value"];
NSArray *innerArray = [NSJSONSerialization JSONObjectWithData:[valueStr dataUsingEncoding:NSUTF8StringEncoding]
options:0
error:&error];
if (innerArray)
// FINALLY! WE HAVE IT!
else
// Report error
break;
else
// report error
【讨论】:
这个解决方案对我来说就像魔术一样,非常感谢,json响应是正确的,但是内部解析数组的想法!好主意,再次感谢你【参考方案3】:After the json is in correct format,i.e.
"errorCd": "00",
"desc": "Success",
"ref": 83,
"statusCode": "1",
"extraData": [
"key": "bal",
"value": "80"
,
"key": "txs",
"value": [
"id": 2268099999,
"amnt": 100.0,
"curr": "JOD",
"sender": "PSPCI",
"receiver": "00962799999992",
"date": "Feb 1, 2016 4:03:25 PM",
"status": 1,
"type": 5,
"fees": 0.0,
"reference": 40
,
"id": 2357099999,
"amnt": 20.0,
"curr": "JOD",
"sender": "00962799999992",
"receiver": "PSPCI",
"date": "Feb 2, 2016 12:52:35 PM",
"status": 1,
"type": 6,
"fees": 0.0,
"reference": 68
]
]
要存储数组,您需要将变量 array 声明为 NSArray 或 NSMutableArray 而不是 NSDictionary
【讨论】:
以上是关于JSON响应中NSDictionary内的对象数组-目标c的主要内容,如果未能解决你的问题,请参考以下文章
iPhone JSON框架不解析不在对象或数组内的JSON字符串对象
在 Swift 中访问 JSON NSDictionary 对象值的简单方法?