如何将json值从数组赋予字典?

时间:2015-08-28 11:47:54

标签: ios objective-c nsarray nsdictionary

我在其存储的json值中有数组。当我尝试将此值赋予字典时给定的错误。

 arrhole{
        "cust_groups" = "";
        "customer_id" = 1;
        "customer_name " = "";
        "date_added" = "<null>";
        "delivery_method" = "";
        fname = kishore;
        gift = "";
        lname = kumar;
        message = "";
        month = "";
        "order_id" = 1;
        "pay_method" = "";
        "payment_firstname" = "";
        "payment_lastname" = "";
        phone = 9043563659;
        region = "";
        reward = "";
        "scheduled_date" = "28/08/2015";
        "sen_email" = "";
        "sen_name" = "";
        "shipping_city" = "";
        "shipping_company" = "";
        "shipping_country" = IND;
        "shipping_region" = 1503;
        "unit_price" = 22;
        voucher = "";
    }

我使用的代码:

 -(void)responseFunction:(NSMutableDictionary *)response
    {
        NSLog(@"response method%@",response);
        BOOL success;
        success =[[response objectForKey:@"success"] boolValue];
        arrHoleOrderDetails =[response objectForKey:@"order"];
        NSLog(@"arrhole%@",arrHoleOrderDetails);
        if(success)
        {
            for (NSDictionary *dic in arrHoleOrderDetails)
            {

                NSLog(@"dic===%@",dic);
            }
        }

输出:

dic===city
 dic===shipping_address
 dic===shipping_lastname
 dic===pay_method
 dic===customer_id
 dic===country
 dic===region
像这样在我的log.i中显示它只显示一些值。

1.这是json响应问题吗?

2.或我的愚蠢错误?

完整回复:

 order =     {
        address = "";
        address1 = "";
        address2 = "";
        affliate = "";
        amount = "";
        city = "";
        comment = "";
        company = "";
        "company_id" = "";
        country = "";
        coupon = "";
        "cust_groups" = "";
        "customer_id" = 2;
        "customer_name " = "";
        "date_added" = "<null>";
        "delivery_method" = "";
        email = "";
        fax = "";
        fname = arun;
        gift = "";
        lname = "";
        message = "";
        month = "";
        "order_id" = 2;
        "pay_method" = "";
        "payment_firstname" = "";
        "payment_lastname" = "";
        phone = "";
        postcode = "";
        product = 4;
        quantity = 5;
        "rec_email" = "";
        "rec_name" = "";
        region = "";
        reward = "";
        "scheduled_date" = "28/08/2015";
        "sen_email" = "";
        "sen_name" = "";
        "shipping_address" = "";
        "shipping_address1" = "";
        "shipping_address2" = "";
        "shipping_city" = "";
        "shipping_company" = "";
        "shipping_country" = "";
        "shipping_firstname" = "";
        "shipping_lastname" = "";
        "shipping_postcode" = "";
        "shipping_region" = "";
        status = "";
        stores = "";
        total = "";
        "unit_price" = 540;
        voucher = "";
    };
    success = 1;
}

2 个答案:

答案 0 :(得分:1)

尝试以下代码

   -(void)responseFunction:(NSMutableDictionary *)response
        {
            NSLog(@"response method%@",response);
            BOOL success;
            success =[[response objectForKey:@"success"] boolValue];
            arrHoleOrderDetails =[response objectForKey:@"order"];
            NSLog(@"arrhole%@",arrHoleOrderDetails);
            if(success)
            {
                yourlable.text = [arrHoleOrderDdetails valueForKey:@"address"];
                yourlabel2.text = [arrHoleOrderDdetails valueForKey:@"address1"];

//In case of integer value try following
yourlbl3.text = [NSString stringWithFormat:@"%d",[arrHoleOrderDdetails valueForKey:@"address1"]];

    //go ahead in same manner.

    }

答案 1 :(得分:0)

这应该有效:

if(success)
{
    //display all keys and values
    for (NSString* key in [arrHoleOrderDetails allKeys])
    {
        NSString* value = [arrHoleOrderDetails objetForKey:key];
        NSLog(@"%@===%@",key, value);
    }

    //access single value
    NSLog(@"%@", arrHoleOrderDetails[@"scheduled_date"]);
}

如果迭代NSDictionary,则需要迭代键并获取给定键的值。 还要记住,NSDictionaries是随机访问的 - 没有任何订单。