Skip to content

Instantly share code, notes, and snippets.

@pipiscrew
Created April 12, 2016 18:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pipiscrew/e15469f199009f4d8f8990bc148453be to your computer and use it in GitHub Desktop.
Save pipiscrew/e15469f199009f4d8f8990bc148453be to your computer and use it in GitHub Desktop.
json decode by PHP array
////////////////// PHP
//get_multi_recordset.php
$brands = getSet($db, "select brand_id from brands where user_id=?", array($user_id));
$categories = getSet($db, "select category_id from categories where user_id=?", array($user_id));
$subcategories = getSet($db, "select subcategory_id from subcategories where user_id=?", array($user_id));
$payments = getSet($db, "select payment_id from payments where user_id=?", array($user_id));
$json = array('brands'=> $brands, 'categories' => $categories, 'subcategories' => $subcategories, 'payments' => $payments);
header("Content-Type: application/json", true);
echo json_encode($json);
////////////////// ANDROID JAVA
//here async, get the response from get_multi_recordset.php, and decode the JSONObject
try {
JSONObject resp = new JSONObject(response);
JSONArray brands = resp.getJSONArray("brands");
String brand_ids="";
for (int i = 0 ; i < brands.length(); i++){
brand_ids+= ((JSONObject) brands.get(i)).getString("brand_id") + ",";
}
if (brand_ids.length() > 0)
brand_ids = brand_ids.substring(0, brand_ids.length() - 1);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//more : http://www.pipiscrew.com/2016/04/android-json-decode-by-php/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment