【问题标题】:Put JSON elements in an array将 JSON 元素放入数组中
【发布时间】:2016-12-14 12:14:35
【问题描述】:

我正在尝试解析 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;
}
)

【问题讨论】:

  • 也许你应该指定语言。
  • @emKaroly 是的,很抱歉,刚刚编辑了它。 :)
  • 您是从响应中获取 JSON 数组还是 JSON 字典?还是虚拟 JSON?
  • 这不是 JSON。这是NSArray 的字符串表示形式。
  • @vadian -_- 这个 JSON 是使用 url 从服务器获取的。

标签: ios objective-c json parsing


【解决方案1】:

在 Swift 中,你可以这样做

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

for dict in responseArray{

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

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

希望这会有所帮助。

【讨论】:

  • 请不要在 Swift 中建议 NSMutableArray
  • 我编辑了你的答案,提供了实际的类型和演员表。
【解决方案2】:

好吧,就这样吧

假设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 也是如此。

【讨论】:

    【解决方案3】:

    谢谢大家的回复, 首先,它不是一个虚拟的 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"];
    

    【讨论】:

    • @Pushkraj 是的,我做到了:)
    • 你的问题和你的答案,奇怪:) 你也可以投票给我。
    • 考虑同步加载数据 (dataWithContentsOfURL) 会导致糟糕的用户体验,因为它会阻塞线程。
    【解决方案4】:
    - (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"]];
        }
    
    
    }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多