将JSON元素放在数组中

时间:2016-12-14 12:14:36

标签: ios objective-c json parsing

Here is the screenshot of the log console

我正在尝试解析JSON,但未能这样做。以下是Json。

我想将每个键的元素放在一个数组中。例如,一个数组中键“itemid”的值和另一个数组中键“itemname”的值。

(
    {
    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;
}
)

4 个答案:

答案 0 :(得分:2)

在Swift中,你可以这样做

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

for dict in responseArray{

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

目标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"])
    ....
}

希望这有帮助。

答案 1 :(得分:1)

好的,这样做

假设NSArray *myResponse是您的数组。

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);

itemname和ordernum也一样。

答案 2 :(得分:1)

嘿谢谢大家回复, 首先,它不是一个虚拟的JSON !!

这是答案。

 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 :(得分: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"]];
    }


}

希望这会对你有所帮助。您的所有数据都在不同的数组中。