如何按顺序迭代所有json值?

时间:2013-07-27 19:46:08

标签: java android json

在我的Android应用程序中,我得到一个json值并通过执行此操作迭代它们

    if (response != null) {
        try {
            JSONObject object = new JSONObject(response);
            Iterator enu = object.keys();
            ArrayList<String> locationList = new ArrayList<String>();
            while(enu.hasNext()) {
                locationList.add(object.getString((String) enu.next()));
            }
            callerContext.DisplayLocations(locationList);
        } catch (JSONException e) {
            Toast.makeText(callerContext, callerContext.getResources().getString(R.string.error_message), Toast.LENGTH_LONG).show();
        }
    }

问题是ArrayList,如果我然后迭代,那么值是以不同的顺序然后我插入PHP代码...

如何以与插入时相同的顺序循环浏览json对象?

感谢。

修改

PHP

            $data = array();
            for ($i = 0; $i < $num_records; $i++) {

                array_push($data, 
                    array("id{$i}" => mysql_result($recordset, $i, 'id')), 
                    array("location{$i}" => mysql_result($recordset, $i, 'location')), 
                    array("date{$i}" => mysql_result($recordset, $i, 'date added'))
                );
            }

3 个答案:

答案 0 :(得分:2)

JSON对象没有或保证订单,如果需要,请使用JSON数组。

答案 1 :(得分:0)

您的Php代码似乎很好。你需要改变你的Java端。试试这个:

BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()))
StringBuilder content = new StringBuilder();

String line;
while (null != (line = br.readLine()) {
   content.append(line);
}
Object obj=JSONValue.parse(content.toString());
JSONArray jArray=(JSONArray)obj;

//test the order
System.out.println(jArray);

// u may iterate it like this:
for (int i = 0; i < jArray.length(); i++) {
    JSONObject row = jArray.getJSONObject(i);
    // do stuff with all rows now for example:
    String location = row.getString("location");
}

请参阅这两个Stackoverflow帖子: JSON Array iteration in Android/JavaParsing a JSON Array from HTTP Response in Java

答案 2 :(得分:0)

我不确定您尝试添加哪些值以添加到LocationList。查看代码,似乎您要将idlocationdate的值添加到LocationList,并且您希望以相同的顺序实现此目的你在他们的PHP中添加它们。如果这不正确,请告诉我,我会相应更新我的答案。无论如何,这应该按顺序添加所有内容:

PHP代码:

$data = array();
for ($i = 0; $i < $num_records; $i++) {

    array_push($data, 
        array("id{$i}" => mysql_result($recordset, $i, 'id')), 
        array("location{$i}" => mysql_result($recordset, $i, 'location')), 
        array("date{$i}" => mysql_result($recordset, $i, 'date added'))
    );
}

// This is what I added. Creates a JSON array.
$jsonArray = json_encode($data);

// Then do whatever you want with $jsonArray, perhaps echo it?

Java代码:

if (response != null) {
    try {
        ArrayList<String> locationList = new ArrayList<String>();
        JSONArray dataArray = new JSONArray(response);
        int length = dataArray.length();

        for (int i = 0; i < length; i++) {
            JSONObject json = dataArray.get(i);
            Iterator enu = json.keys();

            // No need for a loop since JSONObject should only have one key => value pair
            locationList.add(json.getString((String) enu.next()));
        }

        callerContext.DisplayLocations(locationList);
    } catch (JSONException e) {
        Toast.makeText(callerContext, callerContext.getResources().getString(R.string.error_message), Toast.LENGTH_LONG).show();
    }
}