I want to process this JSON result: ["13:00:00","14:00:00"]
in my mobile application, java language.
The JSON might be more than 2 item, or less. Maximum 8 item, 8 hour.
I make a PHP request in my application, send the data which makes a query run, the result is in the JSON result.
Here is my code:
try {
post.setEntity(new UrlEncodedFormEntity(dataToSend)); //post the datas for the query
HttpResponse httpResponse = client.execute(post);
HttpEntity entity = httpResponse.getEntity();
String result = EntityUtils.toString(entity);
JSONObject jObject = new JSONObject(result);
JSONArray jArr = jObject.getJSONArray("dates");
if (jObject.length() != 0) {
for (int i = 0; i < jArr.length(); i++)
{
String[] dates = jArr.getJSONObject(i).getString("dates");
/*Here I don't really have idea what to do
I want a String array which indexes are the result hours
like dates[0] = "13:00:00"*/
}
And the PHP:
$place = $_POST["place"];
$date = $_POST["date"];
$sql = "SELECT appointment FROM dates WHERE place = '$place' AND appointment LIKE '%$date%' ";
$result = mysqli_query($con, $sql);
$datehours = array();
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){
array_push($datehours, $row["appointment"]); //after it datehours like [0] => 2016-04-08 13:00:00
}
$dates = array();
foreach($datehours as $x => $x_value) {
$muvelet = explode(" ",$x_value); //here I select from the timeststamp only the hours
array_push($dates, $muvelet[1]); //$date like [0] => 13:00:00
}
echo json_encode($dates, JSON_UNESCAPED_UNICODE); //the encode is ["13:00:00","14:00:00"]
So how should I process this JSON encode? Should I write somehow else in the PHP? Unfortunately the encode with numeric array isn't a pair like an assoc array ($date["firsthour"] => 13:00:00
in this case the JSON something like ["firsthour" : "13:00:00"]
if I know right, and here I can identify the elements.
Once you receive the response you can use
JsonArray mArray = new JsonArray(<Your response string>);
After that you can iterate through the array as you would a normal array. You can process data as it is, you dont have to change in php.