I have already fetched data from server and passing the data is also populated with NSMutableArray
after NSJSONSerialization
.
Output of array is as follow's:
NSArray *jsonArray = (NSArray *)json;
NSLog(@"Array - %@",jsonArray);
I want to create a new array which will contain all the data of newsletter.
Array - (
{
error = 0;
newsletter = (
{
date = "2015-11-23";
description = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop p";
id = 42;
image = "link/uploads/14482883361619729430.png";
"post_count" = 1;
"posted_by" = admin;
title = Testing;
},
{
date = "2015-11-23";
description = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop p";
id = 43;
image = "link/uploads/14483023241983843096.png";
"post_count" = 0;
"posted_by" = admin;
title = "Lorem Ipsum";
},
I have tried to use the function objectAtIndex
but didn't worked, I am checking the count of jsonArray
.
NSLog(@"count of array - %lu",jsonArray);
it show's 2 as counter.
My Code :
if (responseData != nil) {
NSMutableArray *json = [NSJSONSerialization JSONObjectWithData:responseData //1
options:NSJSONReadingMutableContainers
error:&err];
NSArray *jsonArray = (NSArray *)json;
NSLog(@"Array - %@",jsonArray);
NSLog(@"Count %lu",[jsonArray count]);
}
Question Updated:
I want to populate two arrays 1 will have error = 0;
and the second should need to have all data in newsletter
= (
{
date = "2015-11-23";
description = "Lorem Ipsum is simply...";
id = 42;
image = "link/uploads/14482883361619729430.png";
"post_count" = 1;
"posted_by" = admin;
title = Testing;
}
Hope now question is bit clear...
Thanks in advance for understanding the question.
The question should not be "i want to have two arrays - one with error and one with all the data", as both are not arrays.
The error is just a number, and the newsletter data is a dictionary with key-value pairs that you will access by the key's value, eg
NSString* newsletterDate = newsletterData[@"date"]
So just get the second object of your JSON array - it will be a dictionary, and you can access all of its data by the respective keys.
来源:https://stackoverflow.com/questions/33960370/nsarray-tokenize-on-base-of-a-key