php - 我如何解析json数组?

标签 php android arrays json

我正在构建一个 Android 应用程序,我需要用我的 localhost 中的一些数据填充自定义 ListView 。我正在尝试使用 volley JsonArrayRequest 以 JSONObject 数组的格式获取该数据,但我得到的只是 org.json.JSONException: End of input at character 0 of .这是我的 json 数组请求:

final String URL = "http://10.0.3.2/androidapi/index.php";
        JsonArrayRequest productsReq = new JsonArrayRequest(Method.POST, URL, new Listener<JSONArray>() {

            @Override
            public void onResponse(JSONArray response) {
                Log.d("productTAG", response.toString());
                for(int i = 0; i < response.length(); i++)
                {
                    try 
                    {   
                        JSONObject productObj = response.getJSONObject(i);
                        String title = productObj.getString("title");
                        String description = productObj.getString("description");
                        String category = productObj.getString("category");
                        String subCategory = productObj.getString("subCategory");
                        String size = productObj.getString("size");
                        String price = productObj.getString("price");
                        String thumbnailUrl = productObj.getString("image_one");

                        Product product = new Product(title, thumbnailUrl, description, category, subCategory, size, price);
                        products.add(product);

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

            }
        }, new ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("products_error", error.getMessage().toString());
                error.printStackTrace();
            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();

                params.put("req", "products");
                params.put("category", category);
                params.put("subCategory", subCategory);

                return params;
            }
        };
        VolleyCore.getInstance(getActivity()).addToRequestQueue(productsReq); 

我使用的方法是 POST 并且在 getParams 下我添加了一个名为 req 的标签,它的值为 products 。我正在检查 index.php 中的这个标签。这是我查询表的函数:

public function getItems($category, $subCategory)
    {
        $query = mysql_query("SELECT * FROM items WHERE category = '$category' AND sub_category = '$subCategory'") or die(mysql_error());


        $objects = array();
        if($query)
        {
            while($objects = mysql_fetch_assoc($query))
            {
                $final[] = $objects;
            }
        }
        return $final;

    }

我在 index.php 中检查请求:

if(isset($_POST['req']) && $_POST['req'] == 'products')
    {
        $category = $_POST['category'];
        $subCategory = $_POST['subCategory'];


        $obj = $func->getItems($category, $subCategory);

        echo json_encode($obj);

    }

$func 是类的实例,我在其中保留所有与数据库一起使用的方法(包括 getItems())。如果我使用 volley StringRequest 我确实会收到回复,但是为什么我在使用 JsonArrayRequest 时没有收到回复?我做错了什么?
编辑
响应示例

06-28 15:05:58.750: D/productTAG(3853): 


[  
   {  
      "_id":"2",
      "user_email":"unemail",
      "title":"fsdfsd",
      "description":"dffdhfg",
      "pri‌​ce":"12",
      "quantity":"12",
      "size":"L",
      "category":"Men",
      "sub_category":"Shoes",
      "imag‌​e_one":"base 64 code…",
      "image_two":"base 64 code..",
      "image_three":"base 64 code"
   },
   {  
      "_id":"3",
      "user_email":"unemail",
      "title":"fsdfsd",
      "description":"dffdhfg‌​",
      "price":"12",
      "quantity":"12",
      "size":"L",
      "category":"Men",
      "sub_category":"Shoes"      ‌​,
      "image_one":"base 64 code",
      "image_two":"base 64 code",
      "image_three":"base 64 code "
   }
]


使用 StringRequest

最佳答案

你应该试试这个

@Override
public void onResponse(String response) 
    try {
        JSONArray myArray = new JSONArray(response);

        for(int i = 0; i < myArray.length(); i++)
        {
            JSONObject jObj = myArray.getJSONObject(i);
            String category = jObj.getString("category");
            Log.d("category", category);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

关于php - 我如何解析json数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31100785/

相关文章:

arrays - 如何在 Postgresql 中获取数组的 JavaScript/JSON 数组?

php - 未找到列 : unknown column

php - 在mysql数据库中存储值时改变盐的值

JavaScript 日历无法正常工作

java - eclipse 无法在大型 android 文件上显示为 "build workspace"...?

java - 安卓线程问题

php - 通过 php 使用 youtube-dl 时出现 Python ImportError

android - LocationClient.getLastLocation() 尽管已连接但返回 Null

arrays - 如何使用 Gson 反序列化仅包含值的嵌套数组 json?

javascript - 对象内部的数组:从另一个对象的数组有条件地更新一个对象的数组