Rick
Rick

Reputation: 85

How can I send valid JSON back to iOS application?

I have a problem. I'm trying for weeks now to fetch data from a database, encode it to JSON and then send it back to my iOS application. The problem is that every time the JSON is not valid says http://jsonviewer.stack.hu/ Here is the code I have now:

//connection to the database
 $dbhandle = mysql_connect($hostname, $username, $password) 
 or die("Unable to connect to MySQL");
 //echo "Connected to MySQL<br>";


//select a database to work with
$selected = mysql_select_db("test",$dbhandle) 
or die("Could not select examples");

$result = mysql_query("SELECT * FROM test.debiteur WHERE SORT_NAAM LIKE '%eri%'");



while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

$deb_nr['deb_nr'] = $row['DEB_NR'];
$deb_naam['name'] = $row['DEB_NAAM'];
$deb_adres['adrs'] = $row['DEB_ADRES'];


$testje = array_merge($deb_nr, $deb_naam, $deb_adres);

$testjevervolg = array('klanten' => array($testje));

 sendResponse(200, json_encode($testjevervolg));
 }
 }

This is what it returns:

{
"klanten": [
{
  "deb_nr": "10010",
  "name": "ERIKA Handelsonderneming",
  "adrs": "Aan de Heibloem 17"
}
]
}{
"klanten": [
{
  "deb_nr": "25071",
  "name": "Afdeling Heffing & Invordering",
  "adrs": "Postbus 1275"
}
]
}{
"klanten": [
{
  "deb_nr": "25247",
  "name": "v.d. Heerik b.v.",
  "adrs": "Flemingstraat 3-5"
}
]
}{
"klanten": [
{
  "deb_nr": "25454",
  "name": "Toering Automatisering",
  "adrs": "Appelhof 17a"
}
]
}{
"klanten": [
{
  "deb_nr": "25601",
  "name": "Ratering Bouw & Industrie",
  "adrs": "de Hogenkamp 1"
}
]
}

That is what i get. The problem is, there should be an array of 'klanten' and there should every deb_nr, name and adrs be. Now every thing gets an own 'Klanten' How can is solve this?

Thanks.

Upvotes: 0

Views: 163

Answers (3)

Joshua
Joshua

Reputation: 2451

Created this but haven't tested, currently just used textwrangler maybe it can help you move forward.

Can you try something like this:

$completeJson = array();


while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

$deb_nr['deb_nr'] = $row['DEB_NR'];
$deb_nr['name'] = $row['DEB_NAAM'];
$deb_nr['adrs'] = $row['DEB_ADRES'];


 array_push($completeJson,$deb_nr);


 }
 $testjevervolg = array('klanten' => $completeJson);
 sendResponse(200, json_encode($testjevervolg));

Upvotes: 0

Codemole
Codemole

Reputation: 3189

In order to get following type of reseult:

[
    {
        "deb_nr": "25071",
        "name": "Afdeling Heffing & Invordering",
          "adrs": "Postbus 1275"
    },
    {
        "deb_nr": "25071",
        "name": "Afdeling Heffing & Invordering",
          "adrs": "Postbus 1275"
    },
    ...
]

=============================================

$result = array();
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$result[] = array(
  "deb_nr" => $row['DEB_NR'],
  "name" => $row['DEB_NAAM'],
  "adrs" => $row['DEB_ADRES'],
);
}

header('Content-type: application/json');
echo json_encode($result);

In order to get following type of reseult:

[
    {
        "klanten" : {
            "deb_nr": "25071",
            "name": "Afdeling Heffing & Invordering",
              "adrs": "Postbus 1275"
        },
    }
    {
        "klanten" : {
            "deb_nr": "25071",
            "name": "Afdeling Heffing & Invordering",
              "adrs": "Postbus 1275"
        },
    },
    ...
]

=============================================

$result = array();
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$result[]["klanten"] = array(
  "deb_nr" => $row['DEB_NR'],
  "name" => $row['DEB_NAAM'],
  "adrs" => $row['DEB_ADRES'],
);
}

header('Content-type: application/json');
echo json_encode($result);

I can not assume other type of respone you want so let me know if you want other type of output.

Upvotes: 0

Ruan
Ruan

Reputation: 83

I use this function to return JSON to my app:

function sql2json($query) {
 $data_sql = mysql_query($query) or die("'';//" . mysql_error());
 $json_str = ""; 
 if($total = mysql_num_rows($data_sql)) { 
   $json_str .= "[\n";
    $row_count = 0;    
    while($data = mysql_fetch_assoc($data_sql)) {
        if(count($data) > 1) $json_str .= "{\n";
        $count = 0;
        foreach($data as $key => $value) {
         if(count($data) > 1) $json_str .= "\"$key\":\"$value\"";
         else $json_str .= "\"$value\"";
            $count++;
            if($count < count($data)) $json_str .= ",\n";
        }
        $row_count++;
        if(count($data) > 1) $json_str .= "}\n";
        if($row_count < $total) $json_str .= ",\n";
    }
   $json_str .= "]\n";
  }
  mysql_free_result($data_sql);
  return $json_str;
}

Upvotes: 1

Related Questions