I have an api function where I use Multiple order id if the order is from Multiple restaurant. This is the response I am getting.
["00000303", "00000304"]
Here is my code which returns value in Json
$new_order_ids[] = $order_id;
if(isset($order_id))
{
$order_id = intval($order_id);
$order_id++;
}
else
{
$order_id = 1;
}
$order_id = str_pad($order_id, 8, '0', STR_PAD_LEFT);
$r_data['order_id'] = $new_order_ids;
$json['order_id'] = $order_info['order_id'];
From above code, i get this result ["00000303", "00000304"]
.
and in java, I use this below line of code to get the response.
if(response.has("order_id"))
order_id = response.getString("order_id");
This gives me ["00000303", "00000304"]
How can i have order_id like "00000303"?
Java code
JSONArray orders=response.order_id;
String orderId;
for(int i=0; i<orders.length(); i++){
orderId = orders.getString(i);
// do whatever with the orderId
}
PHP code
function () {
$order_ids = array();
array_push($order_ids, 'order id 1');
array_push($order_ids, 'order id 2');
return json_encode({'order_id', $order_id});
}
Update:
Remember to handle JSONException
when parsing the JSON response in android
try {
JSONArray orders=response.order_id;
String orderId;
for(int i=0; i<orders.length(); i++){
orderId = orders.getString(i);
// do whatever with the orderId
}
}
catch(JSONException err) {
err.printStackTrace();
}
try this
try {
JSONArray orderArray=new JSONArray(response);
for (int i = 0; i < orderArray.length(); i++) {
String orderId=orderArray.getInt(i);
Log.e("orderId", i+"="+orderId);
}
} catch (JSONException e) {
e.printStackTrace();
}