Note that there are some explanatory texts on larger screens.

plurals
  1. POParse json_encoded data into an array to be used for a UIPickerView - Xcode
    text
    copied!<p>I'm looking to populate an array to be used in a pickerview. The data for the array is generated from a database table.</p> <p>I have the following JSON_ENCODED array (created in php):</p> <pre><code>{"result": [ {"id":"3","quivername":"Kite Boarding"}, {"id":"4","quivername":"Live and Die in LA"}, {"id":"14","quivername":"Bahamas Planning"}, {"id":"15","quivername":"My Trip to India"}, {"id":"16","quivername":"Snowboarding"} ] } </code></pre> <p>UPDATE FOR WEBSERVICE PHP CODE</p> <p>I run this function as a webservice:</p> <pre><code>passport($_SESSION['IdUser']) function passport($userid) { $result = query("SELECT id, quivername FROM quivers WHERE userid = $userid"); if (!$result['error']) { print json_encode($result); } else { errorJson('Can not get passports'); } } function query() { global $link; $debug = false; //get the sql query $args = func_get_args(); $sql = array_shift($args); //secure the input for ($i=0;$i&lt;count($args);$i++) { $args[$i] = urldecode($args[$i]); $args[$i] = mysqli_real_escape_string($link, $args[$i]); } //build the final query $sql = vsprintf($sql, $args); if ($debug) print $sql; //execute and fetch the results $result = mysqli_query($link, $sql); if (mysqli_errno($link)==0 &amp;&amp; $result) { $rows = array(); if ($result!==true) while ($d = mysqli_fetch_assoc($result)) { array_push($rows,$d); } //return json return array('result'=&gt;$rows); } else { //error return array('error'=&gt;'Database error'); } } </code></pre> <p>I USE THIS CODE IN XCODE TO CONNECT TO THE WEBSERVICE/WEBSITE USING NSNetworking...</p> <pre><code>-(void)getPassports { //just call the "passport" command from the web API [[API sharedInstance] commandWithParams:[NSMutableDictionary dictionaryWithObjectsAndKeys: @"passport",@"command", nil] onCompletion:^(NSDictionary *json) { //got passports, now need to populate pickerArray1 if (![json objectForKey:@"error"]) { //success, parse json data into array for UIPickerview } else //error, no passports { // error alert } }]; } </code></pre> <p>I need the following:</p> <p>1) how can i populate an NSMutable array with the quivername value? I'm trying to get the result to look like this:</p> <pre><code>pickerArray1 = [[NSMutableArray alloc] initWithObjects:@"Kite Boarding", @"Live and Die in LA", @"Bahamas Planning", @"My Trip to India", @"Snowboarding", nil]; </code></pre> <p>I'm assuming I need to run a for loop which would require me to "count" the number of rows in the array first, but I'm not sure. I don't know how to count the number of rows in the json_array or create an NSMutable array.</p> <p>Thanks in advance...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload