【发布时间】:2017-02-27 07:20:24
【问题描述】:
我想使用 MySql 数据库中的数据创建一个 JSON 文件。
我正在使用以下代码制作一个 json 文件。
<?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);
?>
它给了我以下输出:
[{"id":"1","ques":"New Delhi is capital of India?","ans":"1"},{"id":"2","ques":"India has 5 neighboring countries.","ans":"0"}]
但我希望我的结果喜欢:
{"questions" : {"question" : [{"id":"1","ques":"New Delhi is capital of India?","ans":"1"},{"id":"2","ques":"India has 5 neighboring countries.","ans":"0"}]}
这样我就可以在android中正确使用它了。我该怎么做呢? 有人可以帮忙吗?
或者你能帮我看看如何在不改变我的数组的情况下在 android 中使用它。
提前致谢。
【问题讨论】: