简体   繁体   中英

How to give json values from array to dictionary?

I have array inside its stored json values.when i try to give this value to dictionary its given error.

 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 = "";
    }

This code i have used:

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

OUTPUT:

dic===city
 dic===shipping_address
 dic===shipping_lastname
 dic===pay_method
 dic===customer_id
 dic===country
 dic===region

like this its displaying in my log.i displaying here only some values.

1.Is it json response problem?

2.or its my silly mistake?

Full Response:

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

Try following code

   -(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.

    }

That should work:

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

If you iterate on NSDictionary, you need to iterate on keys and get values for given key. Also remember that NSDictionaries are access randomly - not having any order.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM