1

I m using JSON and while parsing, i m getting the returned value as

> [{"id":"2","name":"a"}, {"id":"3","name":"b"},
> {"id":"104","name":"c"}, {"id":"4","name":"d"}]

I want to have the 'name' in some array, so that i can show the names in picker view.

from array to pickerview, i can perform,but i m getting problem in retrieving the values of name and put into an array.

2
  • I m using JSON and while parsing, i m getting the returned value as I don't quite get this? How do you parse? As a term of parse, after parsing you should have a NSArray object filled with NSDictionary objects with keys id and name, from which you can take the values... Commented Jun 15, 2012 at 10:16
  • Refer to this stackoverflow post: [JSON Parse][1] Hope this will help you [1]: stackoverflow.com/questions/8890808/… Commented Jun 15, 2012 at 10:21

3 Answers 3

1
jsonArray = [{"id":"2","name":"a"}, {"id":"3","name":"b"},
 {"id":"104","name":"c"}, {"id":"4","name":"d"}]

NSMutableArray *nameArray = [[NSMutableArray alloc] initWithCapacity:0];

for(NSMutableDictionary *dict in jsonArray){
   NSString *str =  [dict objectForKey:@"name"];
   if(str){      
         [nameArray addObject:str];
    }
}

This your name array... hope this will help you

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

5 Comments

You took returned json values into jsonArray first? how
The JSON array is same your json value... your json value is an array which contains 4 key... i hope u understood and i m returning only name value in that array... you need it same right??
NSMutableArray *jsonList......thats same as jsonarray.got it.That worked....thank Abhishek.exactly what i was trying.
Hello Abhishek, can we take the whole thing in dictionary? so that, when put the names into pickerview,and onselect ,i can have the id also?
i cant vote up, untill i hv 15...i will put in my reminder....for sure.once i m above 15 i m gonna upvote .
0

You'll need some kind of JSON parser library, which makes a discrete data collection type (for example, an NSDictionary or NSArray) out of the JSON string. I've written one library which I prefer:

https://github.com/H2CO3/CarbonateJSON/

There are also a lot of older and probably better solutions; consider JSONKit:

https://github.com/johnezang/JSONKit

And SBJSON (a. k. a. JSON framework):

https://github.com/stig/json-framework

Hope this helps.

Comments

0

jsonArray = [{"id":"2","name":"a"}, {"id":"3","name":"b"}, {"id":"104","name":"c"}, {"id":"4","name":"d"}]

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

for(int i=0;i<jsonarray.count;i++){
   if([[jsonArray objeactAtIndex:i] objectForKey:@"name"])
   {
          NSString *str =[[jsonArray objeactAtIndex:i] 
          objectForKey:@"name"];
         [nameArray addObject:str];
    }
}

Its Working Properly.

Comments

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.