I wanted to make a JSON file using data from my MySql Database.
I'm using the following code to make a json file.
<?php
$con = mysqli_connect('localhost','root','','mydb1');
$return_arr = array();
$sql = "select * from questions";
$res = mysqli_query($con,$sql);
while($row = mysqli_fetch_assoc($res)){
$row_array['id'] = $row['id'];
$row_array['ques'] = $row['ques'];
$row_array['ans'] = $row['ans'];
array_push($return_arr,$row_array);
}
mysqli_close($con);
$b = json_encode($return_arr);
$f = fopen("test.json","w");
fwrite($f,$b);
fclose($f);
?>
It is giving me the following output :
[{"id":"1","ques":"New Delhi is capital of India?","ans":"1"},{"id":"2","ques":"India has 5 neighboring countries.","ans":"0"}]
But i want my result to like :
{"questions" : {"question" : [{"id":"1","ques":"New Delhi is capital of India?","ans":"1"},{"id":"2","ques":"India has 5 neighboring countries.","ans":"0"}]}
So that i can use it properly in android.How can i do this? Can anyone please help?
Or can you please help me in how can i use it in android without changing my array.
Thanks in advance.