在Objective-C中过滤解析的JSON

在Objective-C中过滤解析的JSON,objective-c,json,Objective C,Json,我想把“lasttradeprice”拿出来,但我似乎不知道如何抓住“lasttradeprice”这一块 我如何“过滤”出“价格”?其他信息都不相关 当前代码: NSURL * url=[NSURL URLWithString:@"https://www.allcrypt.com/api.php?method=singlemarketdata&marketid=672"]; // pass your URL Here. NSData * data=[NSData da

我想把“lasttradeprice”拿出来,但我似乎不知道如何抓住“lasttradeprice”这一块

我如何“过滤”出“价格”?其他信息都不相关

当前代码:

 NSURL * url=[NSURL URLWithString:@"https://www.allcrypt.com/api.php?method=singlemarketdata&marketid=672"];   // pass your URL  Here.

    NSData * data=[NSData dataWithContentsOfURL:url];

    NSError * error;

    NSMutableDictionary  * json = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers error: &error];

    NSLog(@"%@",json);


    NSMutableArray * referanceArray=[[NSMutableArray alloc]init];

    NSMutableArray * periodArray=[[NSMutableArray alloc]init];

    NSArray * responseArr = json[@"lasttradeprice"];

    for(NSDictionary * dict in responseArr)
    {

        [referanceArray addObject:[dict valueForKey:@"lasttradeprice"]];
        [periodArray addObject:[dict valueForKey:@"lasttradeprice"]];

    }


    NSLog(@"%@",referanceArray);
    NSLog(@"%@",periodArray);

注意:请记住,我以前从未使用过JSON,因此请将您的答案简化一点。

您想要的值隐藏在一些字典中。一个普遍的想法可能是递归挖掘,类似这样:

- (BOOL)isCollection:(id)object {
    return [object isKindOfClass:[NSArray self]] || [object isKindOfClass:[NSDictionary self]];
}

- (void)valuesForDeepKey:(id)key in:(id)collection results:(NSMutableArray *)results  {

    if ([collection isKindOfClass:[NSDictionary self]]) {
        NSDictionary *dictionary = (NSDictionary *)collection;
        if (dictionary[key]) [results addObject:dictionary[key]];

        for (id deeperKey in [dictionary allKeys]) {
            if ([self isCollection:dictionary[deeperKey]]) {
                [self valuesForDeepKey:key in:dictionary[deeperKey] results:results];
            }
        }
    } else if ([collection isKindOfClass:[NSArray self]]) {
        NSArray *array = (NSArray *)collection;
        for (id object in array) {
            if ([self isCollection:object]) {
                [self valuesForDeepKey:key in:object results:results];
            }
        }
    }
}
NSMutableArray *a = [NSMutableArray array];
[self valuesForDeepKey:@"lasttradeprice" in:json results:a];
NSLog(@"%@", a);
那么就这样称呼它:

- (BOOL)isCollection:(id)object {
    return [object isKindOfClass:[NSArray self]] || [object isKindOfClass:[NSDictionary self]];
}

- (void)valuesForDeepKey:(id)key in:(id)collection results:(NSMutableArray *)results  {

    if ([collection isKindOfClass:[NSDictionary self]]) {
        NSDictionary *dictionary = (NSDictionary *)collection;
        if (dictionary[key]) [results addObject:dictionary[key]];

        for (id deeperKey in [dictionary allKeys]) {
            if ([self isCollection:dictionary[deeperKey]]) {
                [self valuesForDeepKey:key in:dictionary[deeperKey] results:results];
            }
        }
    } else if ([collection isKindOfClass:[NSArray self]]) {
        NSArray *array = (NSArray *)collection;
        for (id object in array) {
            if ([self isCollection:object]) {
                [self valuesForDeepKey:key in:object results:results];
            }
        }
    }
}
NSMutableArray *a = [NSMutableArray array];
[self valuesForDeepKey:@"lasttradeprice" in:json results:a];
NSLog(@"%@", a);

键值编码提供了一种挖掘数据的简单方法。使用所需值的键路径。例如,您可以使用如下路径“return.markets.OMC.recenttrades”获取最近交易的数组(假设您的代码获取
json
字典):

这比一次只挖掘一个层次要简洁得多

数组为给定键返回的值是数组成员为该键返回的值数组。换句话说,您可以这样做:

NSArray *recentprices = [trades valueForKey:@"price"];
由于这只是关键路径的下一步,您可以将上面的两个操作合并为一个操作:

NSArray *recentprices = [json valueforKeyPath:@"return.markets.OMC.recenttrades.price"];
这里唯一不好的一面是没有真正的错误检查——要么数据符合您的期望,您得到了您的价格数组,要么它在某种程度上不匹配,您得到了
nil
。在某些情况下这很好,而在其他情况下则不太好

结合代码的相关部分,我们得到:

NSURL *url = [NSURL URLWithString:@"https://www.allcrypt.com/api.php?method=singlemarketdata&marketid=672"];
NSData *data = [NSData dataWithContentsOfURL:url];
NSError *error = nil;
NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers error:&error];
NSArray *recentprices = [json valueforKeyPath:@"return.markets.OMC.recenttrades.price"];
更新:我刚刚注意到您想要的是“lasttradeprice”,而不是价格数组。考虑到这一点,使用的关键路径就是“return.markets.OMC.lasttradeprice”,您将返回的值将是一个字符串。因此,将上面的最后一行替换为:

NSString *lastTradePrice = [json valueforKeyPath:@"return.markets.OMC.lasttradeprice"];

嗯,它返回了
(null)
知道为什么吗?它在一个简单的case I设置中工作。让我把你的json拿来,看一下。。。。几分钟。谢谢,只是想让你知道,我正在寻找“lasttradeprice”,就这样,我不需要任何其他东西。这些值包含在数组中。我对代码进行了概括,以处理字典和数组,并在键出现多次时返回一个值数组。在你的URL上测试这个可以得到lasttradeprice的“0.00006000”。我不知道它是如何工作的,我所做的只是将执行行向下移动2行,然后它就修复了它。非常感谢你!对上次编辑进行了回滚。用“明白了,谢谢,再多几个字符”来代替你的问题不是个好主意。这对未来的游客没有帮助。