Shikha Sharma
Shikha Sharma

Reputation: 469

Put JSON elements in an array

Here is the screenshot of the log console

I am trying to parse a JSON, but failed to do so. Below is Json.

I want to put elements for each key in an array. For example values for key "itemid" in one array and values for key "itemname" in another array.

(
    {
    itemid = 2;
    itemname = "BABY & KIDS";
    ordernum = 2;
},
    {
    itemid = 9;
    itemname = BOOKS;
    ordernum = 7;
},
    {
    itemid = 8;
    itemname = "COMPUTERS & GAMING";
    ordernum = 6;
},
    {
    itemid = 1;
    itemname = ELECTRONICS;
    ordernum = 1;
},
    {
    itemid = 5;
    itemname = "HOME & KITCHEN";
    ordernum = 5;
},
    {
    itemid = 3;
    itemname = MEN;
    ordernum = 3;
},
    {
    itemid = 10;
    itemname = "MOBILE & TABLETS";
    ordernum = 8;
},
    {
    itemid = 4;
    itemname = WOMEN;
    ordernum = 4;
}
)

Upvotes: 0

Views: 136

Answers (4)

PiyushRathi
PiyushRathi

Reputation: 959

In Swift, you can do like this

var itemIDArray = [Int]()
var itemNameArray = [String]()

for dict in responseArray{

    itemIDArray.append(dict["itemid"] as! Int)
    itemNameArray.append(dict["itemname"] as! String)
    ....
}

and In Objective C

NSMutableArray *itemIDArray = [[NSMutableArray alloc] init];
NSMutableArray *itemNameArray = [[NSMutableArray alloc] init];

for NSDictionary *dict in responseArary{

    itemIDArray.addObject(dict.valueForKey[@"itemid"])
    itemIDArray.addObject(dict.valueForKey[@"itemname"])
    ....
}

Hope this helps.

Upvotes: 2

dahiya_boy
dahiya_boy

Reputation: 9503

- (void)simpleJsonParsing
{
    //-- Make URL request with server
    NSHTTPURLResponse *response = nil;
    NSString *jsonUrlString = [NSString stringWithFormat:@"http://domain/url_link"];
    NSURL *url = [NSURL URLWithString:[jsonUrlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    //-- Get request and response though URL
    NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];

    //-- JSON Parsing
    NSMutableArray *jsonResult = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil];
    NSLog(@"Result = %@",jsonResult);

    for (NSMutableDictionary *dic in jsonResult)
    {
        NSString *string = dic[@"array"];
        if (string)
        {
            NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
            dic[@"array"] = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        }
        else
        {
            NSLog(@"Error in url response");
        }
    }

    NSMutableArray *arrItemID = [NSMutableArray new];
    NSMutableArray *arritemname = [NSMutableArray new];
    NSMutableArray *arrordernum = [NSMutableArray new];

    for (int i = 0; i< jsonResult.count; i++) {
        [arrItemID addObject:jsonResult[i][@"itemid"]];
        [arritemname addObject:jsonResult[i][@"itemname"]];
        [arrordernum addObject:jsonResult[i][@"ordernum"]];
    }


}

Hope this helps you. Your all data in different array.

Upvotes: 1

Shikha Sharma
Shikha Sharma

Reputation: 469

Hey thank you everyone for responding, First of all it's not a dummy JSON !!

here is the answer.

 NSURL * url=[NSURL URLWithString:@"url"];
NSData * data=[NSData dataWithContentsOfURL:url];
NSError * error;
array = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

    _itemName=[array valueForKey:@"itemname"];
    _itemId=[array valueForKey:@"itemid"];

Upvotes: 1

Pushkraj Lanjekar
Pushkraj Lanjekar

Reputation: 2294

Okay so do this thing

Lets suppose NSArray *myResponse is your array.

NSMutableArray *arrayItemIds = [[NSMutableArray alloc]init];
for (int i=0; i<myResponse.count;i++){
    [arrayItemIds addObject:[[myResponse objectAtIndex:i]valueForKey:@"itemid"]];
}

NSLog(@"My ID List: %@",arrayItemIds);

Same goes for itemname and ordernum.

Upvotes: 1

Related Questions