gpt4 book ai didi

javascript - 返回 JS 对象而不是数组的 php 脚本

转载 作者:行者123 更新时间:2023-11-30 01:15:01 25 4
gpt4 key购买 nike

我想使用 php 脚本从我的本地服务器获取数据,该脚本应该返回一个数据数组而不是对象。

我想使用这个 JavaScript 数组通过 Android 中的 volley 库获取数据以在 ListView 中显示它们,所以这是 php 脚本:

 <?php error_reporting(0); include("db_config.php");

// array for JSON response $response = array();

// get all items from myorder table $result = mysql_query("SELECT * FROM free_lancer")
or die(mysql_error());

if (mysql_num_rows($result) > 0) {

$response["orders"] = array();

while ($row = mysql_fetch_array($result)) {
// temp user array
$item = array();
$item["ID_FL"] = $row["ID_FL"];
$item["FL_LOGIN"] = $row["FL_LOGIN"];

// push ordered items into response array
array_push($response["orders"], $item);
}
// success
// $response["success"] = 1; } else {
// order is empty
$response["success"] = 0;
$response["message"] = "No Items Found"; } // echoing JSON response echo json_encode($response);

?>

这是输出:

{

"orders":[
{
"ID_FL":"1",
"FL_LOGIN":"log"
},
{
"ID_FL":"7",
"FL_LOGIN":"L"
}
]

}

这是我 Activity 中的代码:

private static final String url = "http://192.168.1.5/apps.php";
private ProgressDialog pDialog;
private List<Movie> movieList = new ArrayList<Movie>();
private ListView listView;
private CustomListAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_m);

listView = (ListView) findViewById(R.id.list);
adapter = new CustomListAdapter(this, movieList);
listView.setAdapter(adapter);

pDialog = new ProgressDialog(this);
// Showing progress dialog before making http request
pDialog.setMessage("Loading...");
pDialog.show();



// Creating volley request obj
JsonArrayRequest movieReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();

// Parsing json
for (int i = 0; i < response.length(); i++) {
try {

JSONObject obj = response.getJSONObject(i);
Movie movie = new Movie();
movie.setTitle(obj.getString("FL_NOM_COMPLET"));
movie.setThumbnailUrl(obj.getString("FL_IMAGE"));
movie.setRating(((Number) obj.get("RATING"))
.doubleValue());
movie.setYear(obj.getInt("FL_NUM_TELE"));

// Genre is json array
/* JSONArray genreArry = obj.getJSONArray("FL_description_travail");
ArrayList<String> genre = new ArrayList<String>();
for (int j = 0; j < genreArry.length(); j++) {
genre.add((String) genreArry.get(j));
}*/
movie.setGenre(obj.getString("FL_description_travail"));

// adding movie to movies array
movieList.add(movie);

} catch (JSONException e) {
e.printStackTrace();
}

}

// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
showMessage("error",error.getMessage());
hidePDialog();

}
});

// Adding request to request queue
AppController.getInstance().addToRequestQueue(movieReq);
}
...
}

问题是生成了一个异常,表明输出无法转换为 js 数组

最佳答案

您的JSON 不是JSONArray,而是JSONObject,其字段中包含JSONArray:

{  
"orders":[
{
"ID_FL":"1",
"FL_LOGIN":"log"
},
{
"ID_FL":"7",
"FL_LOGIN":"L"
}
]
}

您的代码应如下所示:

JsonArrayRequest movieReq = new JsonArrayRequest(url,
//here is the change
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
hidePDialog();


// Parsing json
//and here too
JSONArray jsonArray = response.getJSONArray("orders");
for (int i = 0; i < jsonArray.length(); i++) {
try {
//and here also
JSONObject obj = jsonArray.getJSONObject(i);
Movie movie = new Movie();
movie.setTitle(obj.getString("FL_NOM_COMPLET"));
movie.setThumbnailUrl(obj.getString("FL_IMAGE"));
movie.setRating(((Number) obj.get("RATING"))
.doubleValue());
movie.setYear(obj.getInt("FL_NUM_TELE"));

// Genre is json array
/* JSONArray genreArry = obj.getJSONArray("FL_description_travail");
ArrayList<String> genre = new ArrayList<String>();
for (int j = 0; j < genreArry.length(); j++) {
genre.add((String) genreArry.get(j));
}*/
movie.setGenre(obj.getString("FL_description_travail"));

// adding movie to movies array
movieList.add(movie);

} catch (JSONException e) {
e.printStackTrace();
}

}

// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
showMessage("error",error.getMessage());
hidePDialog();

}
});

关于javascript - 返回 JS 对象而不是数组的 php 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37982979/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com