Android数据显示格式错误,从JSONObject到ListView

时间:2016-09-10 11:58:01

标签: android listview

我尝试使用HashMapListview显示2列数据,但效果很好。当我尝试显示超过2列时出现错误。

2_columns.java

public class Main4Activity extends AppCompatActivity {

String url = "http://192.168.1.103/web_service/omg.php/";
private static final String COLUMN_ID = "ID";
private static final String COLUMN_NAME = "Name";

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

    JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

    @Override
    public void onResponse(JSONObject response) {
        List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();

        try {
            Iterator<String> iterator = response.keys();
            while (iterator.hasNext()) {
                String key = iterator.next();
                String value = response.getString(key);

                HashMap<String, String> map = new HashMap<>();
                map.put(COLUMN_ID, key);
                map.put(COLUMN_NAME, value);

                aList.add(map);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        if(aList.size() > 0) {
            String[] from = {COLUMN_ID, COLUMN_NAME};
            int[] to = {R.id.text_id, R.id.text_name};
            SimpleAdapter adapter = new SimpleAdapter(Main4Activity.this, aList, R.layout.list_item, from, to);
            ListView listView = (ListView) findViewById(R.id.listView);
            listView.setAdapter(adapter);
        }
            }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d("error", "error ");
        }
    });
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(jsObjRequest);
}
}

本地主机/使用web_service / omg.php

{
"32":"Western Food",
"33":"Chinese Food",
"34":"Mix Food",
"35":"Japanese Food",
"36":"Korean Food",
"37":"Italian Food",
"38":"German Food"
}

enter image description here

以上编码工作正常。但是当我尝试在多列中显示数据时出现错误,

multiple_columns.java

String url = "http://192.168.1.103/web_service/ohno.php/";

private static final String product_sku = "SKU";
private static final String product_name = "Name";
private static final String product_price = "Price";
private static final String product_quantity = "Quantity";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main5);

    JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();

            try {
                Iterator<String> iterator = response.keys();
                while (iterator.hasNext()) {
                    String key = iterator.next();
                    String value_sku = response.getString(key);
                    String value_name= response.getString(key);
                    String value_price= response.getString(key);
                    String value_quantity= response.getString(key);

                    HashMap<String, String> map = new HashMap<>();
                    map.put(product_sku, value_sku);
                    map.put(product_name, value_name);
                    map.put(product_price, value_price);
                    map.put(product_quantity, value_quantity);

                    aList.add(map);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            if(aList.size() > 0) {
                String[] from = {product_sku,product_name, product_price,product_quantity};
                int[] to = {R.id.sku,R.id.name, R.id.price,R.id.quantity};
                SimpleAdapter adapter = new SimpleAdapter(Main5Activity.this, aList, R.layout.list_item2, from, to);
                ListView listView = (ListView) findViewById(R.id.listView);
                listView.setAdapter(adapter);
            }
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d("error", "error ");
        }
    });
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(jsObjRequest);
}

本地主机/使用web_service / ohno.php

{
"482":["1","Chicken Rice","1","1"],
"483":["1","French Fries","1","1"],
"484":["1","apple","1","1"],
"492":["1","western+italian","1","1"],
"493":["1","no_cat","1","1"]
}

enter image description here

从上面的屏幕截图中可以看出,我希望Key为482,483,484 ......并且值为右侧。编码有什么问题?

2 个答案:

答案 0 :(得分:1)

似乎JSON中的值为JSONArray s,因此response.getString(key)会将整个数组作为String返回。

致电getJSONArray()以获取JSONArray的值,以便您可以单独处理这些项目。

这样的事情应该有效:

try {
    Iterator<String> iterator = response.keys();
    while (iterator.hasNext()) {
        String key = iterator.next();
        JSONArray array = response.getJSONArray(key);
        HashMap<String, String> map = new HashMap<>();

        map.put(product_sku, array.getString(0));
        map.put(product_name, array.getString(1));
        map.put(product_price, array.getString(2));
        map.put(product_quantity, array.getString(3));

        aList.add(map);
    }
} catch (JSONException e) {
    e.printStackTrace();
}

答案 1 :(得分:1)

您正在使用JSONArray解析JSONObject这是错误的。请尝试以下

List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();

try {
    Iterator<String> iterator = response.keys();
    while (iterator.hasNext()) {
        String key = iterator.next();
        JSONArray jsonArray = response.getJSONArray(key);
        String value_sku = jsonArray.get(0).toString();
        String value_name = jsonArray.get(1).toString();
        String value_price = jsonArray.get(2).toString();
        String value_quantity = jsonArray.get(3).toString();

        HashMap<String, String> map = new HashMap<>();
        map.put(product_sku, value_sku);
        map.put(product_name, value_name);
        map.put(product_price, value_price);
        map.put(product_quantity, value_quantity);

        aList.add(map);
    }
}