我有一个需要从我的在线数据库获取数据库的android项目。我已经开发了Android代码,但现在我被困在PHP代码上,以便获取我的在线数据库。
好吧,我正在使用JSON来获取数据库,所以我在Android端编码就像这样:
JSONArray j = new JSONArray(total);
JSONObject Obj = j.getJSONObject(i);
item.setId(Obj.getString(JF_ID));
item.setTitle(Obj.getString(JF_TITLE));
item.setAddress(Obj.getString(JF_ADDRESS));
item.setTelephoneNumber(Obj.optString(JF_TELEPHONE, NO_DATA_FOUND));
item.setEmailAddress(Obj.optString(JF_EMAIL, NO_DATA_FOUND));
item.setWebUrl(Obj.optString(JF_WEB, NO_DATA_FOUND));
item.setFacebookUrl(Obj.optString(JF_FACEBOOK, NO_DATA_FOUND));
item.setLatitude(Obj.optDouble(JF_LATITUDE, NULL_LOCATION));
item.setLongitude(Obj.optDouble(JF_LONGITUDE, NULL_LOCATION));
try {
item.setRating(Float.parseFloat(Obj.optString(JF_RATING, NO_DATA_FOUND)));
} catch (NumberFormatException e) {
item.setRating(0.0f);
}
try {
item.setRatingCount(Integer.parseInt(Obj.optString(JF_RATING_COUNT, NO_DATA_FOUND)));
} catch (NumberFormatException e) {
item.setRatingCount(0);
}
item.setTagLine(Obj.optString(JF_TAG_LINE, NO_DATA_FOUND));
item.setDescription(Obj.optString(JF_DESCRIPTION, NO_DATA_FOUND));
item.setVerification(Obj.optString(JF_VERIFICATION, NO_DATA_FOUND).equals("1") ? true : false);
JSONArray imgArr = Obj.getJSONArray("thumbImage");
String[] imageThumb = new String[imgArr.length()];
你可以在那里看到,我的模式是首先获取数组并再次获取对象和最后一次获取数组。
换句话说,我希望我的结果看起来像这个例子:
["arrayA":{"id":"1", "year":"2016","thumbnail":["a.jpg","b.jpg","c.jpg"]}]
这是问题所在,我不能让服务器端的PHP代码返回我想要的模式。它总是重新调整数组>对象就是它,在Object之后没有再有Array。
这是我到目前为止编写的PHP代码:
$qu=mysqli_query($con,"select * from car_rental where rental_id='1'");
while ($row = mysqli_fetch_assoc($qu)) {
$res [] =array($row,
$thumbImage[] =
array( "t1"=>$row['thumbImage1'],
"t2"=>$row['thumbImage2'],
"t3"=>$row['thumbImage3']
) );
}
$json["thumb"] = $res;
echo json_encode($json);
请高手帮帮我。 感谢。
答案 0 :(得分:0)
如你所说,你想要输出
["arrayA":{"id":"1", "year":"2016","thumbnail":["a.jpg","b.jpg","c.jpg"]}]
试试这个PHP代码
$qu=mysqli_query($con,"select * from car_rental where rental_id='1'");
$res = array();
$singleRecordSet = array();
while ($row = mysqli_fetch_assoc($qu)) {
$singleRecordSet = $row;
$multiRecordSet = array('thumb'=>array(
"t1"=>$row['thumbImage1'],
"t2"=>$row['thumbImage2'],
"t3"=>$row['thumbImage3']
));
$res = array_merge($singleRecordSet,$multiRecordSet);
}
$json["arrayA"] = $res;
echo json_encode($json);