java - Android从json中获取数据并排序

标签 java android json

  • 我正在尝试从 json 中获取数据。我可以获得第一状态的数据。
  • 但是如何获取“升序”和“降序”数据并将其显示在 ListView 中的另一个 Activity 上?

这是我的 Json

[{"category_name":"Food","filter_type":"Sort by","field_name":"","type":"VALUE","table_name":"","item_list":["Ascending","Descending"]}

这是我的 Java 代码

if (jsonStr != null) {
    try {

        foods = new JSONArray(jsonStr);


        // looping through All Contacts
        for (int i = 0; i < foods.length(); i++) {
            JSONObject c = foods.getJSONObject(i);

            if(c.getString("category_name").equals("Food")) {

                String category_name = c.getString(TAG_CATEGORY_NAME);
                String table_name = c.getString(TAG_TABLE_NAME);
                String item_list = c.getString(TAG_ITEM_LIST);

                // tmp hashmap for single contact
                HashMap<String, String> contact = new HashMap<String, String>();

                // adding each child node to HashMap key => value
                contact.put(TAG_CATEGORY_NAME, category_name);
                contact.put(TAG_TABLE_NAME, table_name);
                contact.put(TAG_ITEM_LIST, item_list);

                // adding contact to contact list
                foodlistfilter.add(contact);

            }
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
} else {
    Log.e("ServiceHandler", "Couldn't get any data from the url");
}

我正在努力学习本教程 http://www.androidhive.info/2012/01/android-json-parsing-tutorial/ , 但我还是不太明白。

最佳答案

让我解释一下。

[ 表示它是一个数组。 { 是一个对象。

在您的例子中,它是一个数组,其中包含一个名为 category_name、filter_type 和 field_name 的 -JSONObject。 type 和 table_name 以及一个带有对象 item_list 的新 jsonarray。

如何解析这个字符串?

这是一个例子:

String str = "[{"category_name":"Food","filter_type":"Sort by","field_name":"","type":"VALUE","table_name":"","item_list":["Ascending","Descending"]}";

JSONArray jsonArray = new JSONArray(str);
//now it holds the JSONObject.
for (int i = 0; i<= jsonArray.length(); i++) {
 //now we loop through and get the jsonObject
 JSONObject jsonObj = new JSONObject(jsonArray.getJsonObject(i));
 //now it contains your data.
 Log.d("Category_nameValue=", jsonObj.getString("category_name"));
 //now we want to get the array from the item_list. 
 JSONArray itemList = new JSONArray(jsonObj.getString("item_list")); 
 //now itemList.getString(1);  === Ascending while itemList.getString(2) == Descending



 //now itemList contains several new objects which can also be looped as the parent one.
}

既然您现在知道如何创建 JSONArray,就可以开始对其进行排序了。

这已在 Android how to sort JSONArray of JSONObjects 得到解答

如果您想将这些数据发送到另一个 Activity,您可以使用 JSONArray.toString() 方法并通过 Intents 发送它。

这在 Pass a String from one Activity to another Activity in Android 中很容易解释。

希望这对您有所帮助。

关于java - Android从json中获取数据并排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24917110/

相关文章:

android - 在 Android 5 中将视频文件存储在 SD 卡上

php - JSON 从 URL 而不是从 MySQL 工作

java - JSON 2 POJO 离线

python - 在 Tweepy 中以可用形式 (JSON) 提取数据

java - 在java 2d中采摘

java - byte 加 byte ?所有操作数自动转换为 int ?

android - onclick 通知,在 Android 中启动一个 Activity

java - 从外部 jar 加载类时,NoClassDefFoundError 仅适用于少数类

java - 蓝牙 socket 未连接,无法配对设备

java - 从 Activity 中搜索 fragment ID