0

in response, i am getting gas_cylinders key as single array object

"gas_cylinders":["[{\"quantity\":\"2\",\"name\":\"Medium Blue\",\"price\":\"100.0\",\"total_price\":\"200.0\"},{\"quantity\":\"3\",\"name\":\"Green\",\"price\":\"100.0\",\"total_price\":\"300.0\"},{\"quantity\":\"1\",\"name\":\"Dark Green\",\"price\":\"100.0\",\"total_price\":\"100.0\"}]"]

Note:- tripDictionary contain below data..

(lldb) po tripDictionary 
{
    "gas_cylinder_total" = 600;
    "gas_cylinders" =     (
        "[{\"quantity\":\"2\",\"name\":\"Medium Blue\",\"price\":\"100.0\",\"total_price\":\"200.0\"},{\"quantity\":\"3\",\"name\":\"Green\",\"price\":\"100.0\",\"total_price\":\"300.0\"},{\"quantity\":\"1\",\"name\":\"Dark Green\",\"price\":\"100.0\",\"total_price\":\"100.0\"}]"
    );
}

i am taking gas_cylinders like below way..

NSArray *arr = [tripDictionary valueForKey:@"gas_cylinders"];
if (arr && arr.count > 0) {
   NSLog(@"first obj = %@",arr[0]);
}

output of above NSLog like..

first obj = [{"quantity":"2","name":"Medium Blue","price":"100.0","total_price":"200.0"},{"quantity":"3","name":"Green","price":"100.0","total_price":"300.0"},{"quantity":"1","name":"Dark Green","price":"100.0","total_price":"100.0"}]

how can i get this object in NSMutableArray ?

4
  • Please update your question showing where tripDictionary comes from. Commented Mar 12, 2018 at 14:33
  • @rmaddy updated Commented Mar 12, 2018 at 14:36
  • Please don't post pictures of text, post actual text. It's easier to read, it's searchable, and the text can be referenced. Commented Mar 12, 2018 at 14:38
  • @rmaddy okay edited Commented Mar 12, 2018 at 14:41

1 Answer 1

2

The data is not what you think it is. The value of the gas_cylinders key is an array of JSON string.

NSArray *cylinders = tripDictionary[@"gas_cylinders"];
NSString *firstCylinder = cylinders[0];

At this point, firstCylinder is a JSON string. You need to parse that JSON string to get the desired array of dictionaries contained in the JSON string.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks.. after your given answer, converted into NSArray and get desired output... NSData* data = [firstCylinder dataUsingEncoding:NSUTF8StringEncoding]; NSArray *values = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.