NSDictionary用于获取Nsarray中的键值数据

时间:2013-09-02 10:47:25

标签: ios cocoa-touch nsdictionary

我在Nsarray中有一些值,使用键值(NSDictionary方法),我需要分别在标签中显示这些值,这是我的数组值。

( "", "user_currency_code=USD", "user_currency_symbol=$", "usertypecode=ROBT", "player_level=1", "player_isblocked=false", "lang=en", "respond=true", "flylogin=false", "str=PageEnd", "user_balance=100000394.90", "user_bonus_balance=0.00", "first_name=test", "cash_balance=100000394.90", "coupon_balance=0.00", "str=PageEnd", "remain_GameCount=0", "remain_PTAmount=0.00", "real_user_balance=100000394.90", "fun_user_balance=95725.15", "user_bonus_balance=0.00", "bonusstatus=false", "bonus_id=null" )

不使用索引值我需要显示标签中的值示例dis值(user_currency_code=USD)键值为(user_currency_code) value(USD),所以最后USD我需要在标签中显示,就像使用我将显示的键值一样标签

3 个答案:

答案 0 :(得分:1)

根据我的理解

( "", "user_currency_code=USD", "user_currency_symbol=$", "usertypecode=ROBT", "player_level=1", "player_isblocked=false", "lang=en", "respond=true", "flylogin=false", "str=PageEnd", "user_balance=100000394.90", "user_bonus_balance=0.00", "first_name=test", "cash_balance=100000394.90", "coupon_balance=0.00", "str=PageEnd", "remain_GameCount=0", "remain_PTAmount=0.00", "real_user_balance=100000394.90", "fun_user_balance=95725.15", "user_bonus_balance=0.00", "bonusstatus=false", "bonus_id=null" ) 

它看起来不像键值编码。要从中获取价值,您需要应用支票数量和数量。需要从字符串中获取子字符串以获取实际值。您需要更改每个键值,如

("user_currency_code":"USD")

如果我做错了,请告诉我

答案 1 :(得分:1)

您需要在收到数据后立即将此数组转换为字典,并丢弃该数组,因为它没用:

#import <Foundation/Foundation.h>

int main(int argc, const char **argv) {
    @autoreleasepool {
        NSArray *arr = [NSArray arrayWithObjects:@"", @"user_ucurrency_code=USD", @"user_currency_symbol=$",
            @"usertypecode=ROBT", @"player_level=1", @"player_isblocked=false", @"lang=en",
            @"respond=true", @"flylogin=false", @"str=PageEnd", @"user_balance=100000394.90",
            @"user_bonus_balance=0.00", @"first_name=test", @"cash_balance=100000394.90",
            @"coupon_balance=0.00", @"str=PageEnd", @"remain_GameCount=0", @"remain_PTAmount=0.00",
            @"real_user_balance=100000394.90", @"fun_user_balance=95725.15", @"user_bonus_balance=0.00",
            @"bonusstatus=false", @"bonus_id=null", nil];

        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        for (NSString *str in arr) {
            NSArray *elements = [str componentsSeparatedByString:@"="];
            if ([elements count] != 2)
                continue;
            [dict setValue:[elements objectAtIndex:1]
                    forKey:[elements objectAtIndex:0]];
        }

        for (NSString *key in dict) {
            NSLog(@"'%@' = '%@'", key, [dict objectForKey:key]);    // or valueForKey:
        }
    }
    return 0;
}

$ clang -o test test.m -fobjc-arc -framework Foundation
$ ./test
2013-09-02 12:01:53.811 test[51144:707] 'bonusstatus' = 'false'
2013-09-02 12:01:53.836 test[51144:707] 'lang' = 'en'
2013-09-02 12:01:53.837 test[51144:707] 'cash_balance' = '100000394.90'
2013-09-02 12:01:53.837 test[51144:707] 'flylogin' = 'false'
2013-09-02 12:01:53.838 test[51144:707] 'str' = 'PageEnd'
2013-09-02 12:01:53.838 test[51144:707] 'usertypecode' = 'ROBT'
2013-09-02 12:01:53.839 test[51144:707] 'player_isblocked' = 'false'
2013-09-02 12:01:53.839 test[51144:707] 'player_level' = '1'
2013-09-02 12:01:53.839 test[51144:707] 'respond' = 'true'
2013-09-02 12:01:53.840 test[51144:707] 'remain_PTAmount' = '0.00'
2013-09-02 12:01:53.840 test[51144:707] 'fun_user_balance' = '95725.15'
2013-09-02 12:01:53.841 test[51144:707] 'remain_GameCount' = '0'
2013-09-02 12:01:53.841 test[51144:707] 'user_balance' = '100000394.90'
2013-09-02 12:01:53.842 test[51144:707] 'user_currency_symbol' = '$'
2013-09-02 12:01:53.842 test[51144:707] 'bonus_id' = 'null'
2013-09-02 12:01:53.842 test[51144:707] 'coupon_balance' = '0.00'
2013-09-02 12:01:53.843 test[51144:707] 'real_user_balance' = '100000394.90'
2013-09-02 12:01:53.843 test[51144:707] 'user_bonus_balance' = '0.00'
2013-09-02 12:01:53.848 test[51144:707] 'first_name' = 'test'
2013-09-02 12:01:53.871 test[51144:707] 'user_ucurrency_code' = 'USD'

答案 2 :(得分:0)

首先你用字典数组看起来像这样: -

(
        {
        "player_isblocked" = false;
        "player_level" = 1;
        "user_currency_code" = USD;
        "user_currency_symbol" = "$";
        usertypecode = ROBT;
    }
}

我使用此代码测试它: -

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

     NSMutableDictionary *dOfPerson=[NSMutableDictionary dictionary];

    [dOfPerson setObject:[NSString stringWithFormat:@"USD"] forKey:@"user_currency_code"];
    [dOfPerson setObject:[NSString stringWithFormat:@"$"] forKey:@"user_currency_symbol"];
    [dOfPerson setObject:[NSString stringWithFormat:@"ROBT"] forKey:@"usertypecode"];
    [dOfPerson setObject:[NSString stringWithFormat:@"1"] forKey:@"player_level"];
    [dOfPerson setObject:[NSString stringWithFormat:@"false"] forKey:@"player_isblocked"];


    [ar addObject:dOfPerson];

    NSLog(@"ar === %@",ar);

然后你必须使用这个例子来证明这一点: -

yourString= [[yourArray objectAtIndex:0(here is item of index)]objectForKey:@"user_currency_code"];// user_currency_code you have write your want key



NSLog(@"%@ =====",[[ar objectAtIndex:0]objectForKey:@"user_currency_code"]);

输出是: -

USD 

或者你也可以喜欢下面的方式

for (NSDictionary *dict in yourArray) {

// here you get with dic all value here try with nslog like
 NSLog(@"%@ =====",[dict valueForKey:@"yourKey");//your is is user_currency_code,user_currency_symbol etc
}