0

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;
}
)
8
  • Maybe you should specify language. Commented Dec 14, 2016 at 12:16
  • @emKaroly yeah am sorry, just edited it. :) Commented Dec 14, 2016 at 12:18
  • Are you getting JSON array or JSON dictionary from response ? Or is it dummy JSON ? Commented Dec 14, 2016 at 12:22
  • 2
    This is not JSON. It's the string representation of an NSArray. Commented Dec 14, 2016 at 12:22
  • @vadian -_- this JSON is Fetched from server using url. Commented Dec 14, 2016 at 12:29

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Please do not suggest NSMutableArray in Swift.
I edited your answer providing the actual types and casts.
1

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.

Comments

1

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"];

3 Comments

@Pushkraj yes i did :)
Your question and your answer, strange :) You can upvote mine also.
Consider that loading data synchronously (dataWithContentsOfURL) causes bad user experience because it blocks the thread.
1
- (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.

Comments

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.