Reputation: 33
I am trying to obtain a list of objects from NSDictionary
's JSON response then store it in NSMutableArray
but it keeps giving me only one object in the array, Can you help me with a sample code please,
Here is the json response:
{
"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}]"
}
]
}
I am trying to obtain the list in the value for the key txs.
This is what am trying to do:
-(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;
}
Upvotes: 2
Views: 2064
Reputation: 122391
Well I would say that the JSON is unusual; it looks like it can be a stringified number ("80"
) or a JSON array.
You need to further parse the inner array, with:
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
}
Upvotes: 0
Reputation: 24341
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
}
]
}
]
}
to store the array you need to declare the variable array as a NSArray or NSMutableArray and not NSDictionary
Upvotes: 0
Reputation: 19310
The JSON data is invalid as value
has an object inside "
You should send your JSON like this
{
"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
}
]
}
]
}
Upvotes: 2