How to get Array inside fields from JSON Structure in ios

Hi i am beginner in ios and in my project i am integrating services with my app but i am struggling to integrate some service fields with my app

Please see below JSON structure there i want to get all "languages" Array inside elements but i am not understand how to get those fields please help me some one

Json stucture:-
 - loans: [
            
        - {
            id: 983381,
            name: "America",
            description: 
        {
            languages: 
        [
            "English"
            ]
            },

        },

         - {
             id: 983382,
            name: "Jarmani",
            description: 
        {
            languages: 
        [
            "Jarman"
            ]
            },

        },

     - {
             id: 983383,
            name: "Rasya",
            description: 
        {
            languages: 
        [
            "Rasya"
            ]
            },

        },
    ]
my code:-
NSMutableArray * array1 = [mainDictyionary objectForKey:@"loans"];

    for (NSDictionary * obj in array1) {

       MainArray = [obj objectForKey:@"languages"];
    }

NSLog(@"so finally  array values are  %@",MainArray);

Answers


You want to

  1. Declare MainArray as a NSMutableArray: MainArray = [[NSMutableArray alloc] init];
  2. Now you will be adding the "languages" NSArrays to a 'mother-array', I'm guessing that is not what you want
  3. So you want to iterate over the [obj objectForKey:@"languages"] array and call addObject on the MainArray for every NSString value you'll encounter in the [obj objectForKey:@"languages"]-iteration.
  4. You'll end up with a mutable NSArray that has all the languages in it, as NSStrings

If your JSON has a format like:

{
    "loans": [{
        "id": "983381",
        "name": "America",
        "description": {
            "languages": [
                "English"
            ]
        }
    }, {
        "id": "983382",
        "name": "Jarmani",
        "description": {
            "languages": [
                "Jarman"
            ]
        }
    }]
}

So next code will give you what you want

NSArray *languagesArrays = [json valueForKeyPath:@"loans.description.languages"];

NSMutableSet *languages = [NSMutableSet new]; 
for (NSArray *languageArray in languagesArrays) {
     [languages addObjectsFromArray:languageArray]; 
}

But I strongly recommend you to use JSONModel to represent objects from your json response


Actually you are getting the value upto languages and not upto "english" or anyother. And also you are assigning the values to MainArray, so it will not add all the values it will add last value of the loop.

NSMutableArray * array1 = [mainDictyionary objectForKey:@"loans"];

NSMutableArray *MainArray = [NSMutableArray alloc] init];

    for (NSDictionary * obj in array1) {
       NSDictionary *descriptionDictionary = [obj objectForKey:@"description"];
       [MainArray addObject:[obj objectForKey:@"languages"]];
    }

  NSLog(@"so finally  array values are  %@", MainArray);

Need Your Help

Load a big plist from url in background

ios xcode load plist grand-central-dispatch

I load a big plist file from url and I must wait for some seconds before I can use the application. Is there some solution? How it can be loaded in background? Is GCD what I need? How it can be

textinput() equivalent in python 2.x

python-2.7 turtle-graphics

I need to accept info from the user in python's turtle. however the turtle.textinput function only works in versions 3.x and upwards is there an alternative I can use in version 2.X ?