2

I have an array and I want to split that array into 3 parts or 3 arrays.

1st array contains -> AppName
2nd array contains -> Description
3rd array contains -> Icon

Here is the json array I want to split,

Deviceinfo =   (
                {
            Appname = App;
            Description = "This is test app";
            Icon = "57.png";
        }
    );
}

Here is my code for this,

NSMutableArray *firstArray = [NSMutableArray array];
NSMutableArray *secondArray = [NSMutableArray array];
NSMutableArray *thirdArray = [NSMutableArray array];

for (int i = 0; i < [json count]; i++) {
    NSArray *tempArray = [[json objectAtIndex:i]componentsSeparatedByString:@""];
    [firstArray addObject:[tempArray objectAtIndex:0]];
    [secondArray addObject:[tempArray objectAtIndex:1]];
    if ([tempArray count] == 3)
    {
        [thirdArray addObject:[tempArray objectAtIndex:2]];
    }
}
NSLog(@"yourArray: %@\nfirst: %@\nsecond: %@\nthird: %@", json, firstArray, secondArray, thirdArray);

I observe a crash in the code at this line,

NSArray *tempArray = [[json objectAtIndex:i]componentsSeparatedByString:@""];

I don't understand what is going wrong here. Any pointers to solve this issue?

1
  • You do not have a JSON array. What you posted is a single dictionary with one key/value pair. The value is an array containing one dictionary. The inner dictionary has 3 key/value pairs. Commented Sep 3, 2013 at 5:52

3 Answers 3

1

I think you can using below code i hope this help's you :-

NSMutableArray *firstArray = [NSMutableArray array];
NSMutableArray *secondArray = [NSMutableArray array];
NSMutableArray *thirdArray = [NSMutableArray array];

NSDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData: jsonResponse options: NSJSONReadingMutableContainers error: &e]; 

 //here is first i load with Dicutionary bcz if into your Json you have may be multiple Dictuionary so you then you can load purticular dictionary as bellow line 

EDIT

  NSArray * responseArr = jsonArray[@"Deviceinfo"];

  firstArray = [responseArr valueForKey:@"Appname"];
  secondArray = [responseArr valueForKey:@"Description"];
  thirdArray = [responseArr valueForKey:@"Icon"];

if you have multiple Deviceinfo dictionary in to your Json then you could use For loop

 //    NSArray * responseArr = jsonArray[@"Deviceinfo"];
 //      for (NSDictionary *dict in responseArr) {

 //        [firstArray addObject:[dict valueForKey:@"Appname"];
 //        [secondArray addObject:[dict valueForKey:@"Description"];
 //       [thirdArray addObject:[dict valueForKey:@"Icon"];
 //     }
Sign up to request clarification or add additional context in comments.

5 Comments

@H2CO3 thx for edit i already given answer with valueForKey
Couldn't you eliminate the loop and do: NSArray *firstArray = [responseArr valueForKey:@"Appname"];?
@rmaddy yes i edit that for loop not requered bcz there is only one dictionary of Deviceinfo so we can use as par your suggestion for loop only req while in json have multiple json dictionary. thank you for your guidance :)
You don't need the loop even if there are multiple dictionaries in the array. That's the point of valueForKey:. It will grab the value from all of the dictionaries and give you an array back.
well i follow this logic that you already put as an Answer take a look stackoverflow.com/questions/18415329/…
0
NSMutableArray *firstArray = [[NSMutableArray alloc]init];
NSMutableArray *secondArray = [[NSMutableArray alloc]init];
NSMutableArray *thirdArray = [[NSMutableArray alloc]init];

NSDictionary *list =[NSJSONSerialization JSONObjectWithData:data
                                                                options:kNilOptions error:&error];

 NSArray * tempArray = jsonArray[@"Deviceinfo"];
for (NSDictionary *list in tempArray)
{
  [firstArray addObject:[list objectForKey:@"Appname"];
   [secondArray addObject:[list objectForKey:@"Description"];
   [thirdArray addObject:[list objectForKey:@"Icon"];
 }

Then try

NSLog(@"yourArray: %@\nfirst: %@\nsecond: %@\nthird: %@", tempArray, firstArray, secondArray, thirdArray);

2 Comments

valueForKey: (lowercase, by the way), is KVO. To access an object from a dictionary you should use objectForKey: instead.
@JustSid, changed, thnx for info.
0
NSData *jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://url.to.your.json"]];

NSArray *jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];

NSMutableArray *appNameArray = [NSMutableArray array];
NSMutableArray *discriptionArray = [NSMutableArray array];
NSMutableArray *iconArray = [NSMutableArray array];

for(NSDictionary *dictionary in jsonObjects)
{
     [appNameArray adddObject:[dictionary valueForKey:@"Appname"];
     [appNameArray adddObject:[dictionary valueForKey:@"Description"];
     [appNameArray adddObject:[dictionary valueForKey:@"Icon"];
}

5 Comments

can you please try valueForKey instead of objectForKey
@JitendraDeore Oh yes, it does work. Apple's code is of excellent high quality. It's just that you perhaps don't know how to use it properly. It would be beneficial if you could elaborate as to how it is "not working", because we are not mind readers, and then we could actually help you solve your problem.
Will re-upvote when you fix valueForKey:. Like @H2CO3 pointed out, objectForKey: works, valueForKey: on the other hand is for KVO and using it to access objects in a dictionary is wrong.
@JitendraDeore So does objectForKey:.
@JustSid Well, actually, it's not wrong, it's just not preferred, because it's confusing. valueForKey: works just like objectForKey: does, except when the key is a string and it starts with an @ character, because then it does indeed return KVO values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.