如何从Json的android系统中过滤数据? [英] How to filter data from Json in android?

查看:271
本文介绍了如何从Json的android系统中过滤数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到的数据的 JSON 喜欢

{"Users":[
{"category_id":"1","user_email":"a@a.com"},
{"category_id":"5","user_email":"a@b.com"},
{"category_id":"1","user_email":"a@c.com"},
{"category_id":"5","user_email":"a@d.com"},
{"category_id":"3","user_email":"a@d.com"}]}

现在我的问题是能有可能在机器人过滤器CATEGORY_ID 例如,如果我选择1从我的微调,那么它应该是返回此JSON基地数据是内容CATEGORY_ID = 1的例子

now my question is can it is possible in android to filter this json base on category_id for example if i am select 1 from my spinner then it should be return the data which is content the category_id = 1 example

{"Users":[{"category_id":"1","user_email":"a@a.com"},
{"category_id":"1","user_email":"a@c.com"}]}

如果选择3,那么它应该返回的是内容的用户的CATEGORY_ID = 3

if select 3 then it should return the user which is content the category_id = 3

{"Users":[
{"category_id":"3","user_email":"a@d.com"}]}

我只是想知道有没有什么方法是在机器人可这样我就可以很容易地过滤 数据。

i just want to know is there any method is available in android so i can easily filter the data.

推荐答案

首先,您需要在您的活动创建变量

First you need to create your variables in your activity

//URL to get JSON
private static String url = "your url to parse data";

//JSON Node Names
private static final String TAG_USERS = "users";
private static final String TAG_CATEGORY_ID = "category_id";
private static final String TAG_USER_EMAIL = "user_email";

// Data JSONArray
JSONArray users = null;

//HashMap to keep your data
ArrayList<HashMap<String, String>> userList;

里面的onCreate()函数实例化用户列表

inside onCreate() function instantiate your userList

userList = new ArrayList<HashMap<String, String)>>();

那么你需要分析你的数据并填充此ArrayList中doInBackground()函数

then you need to parse your data and populate this ArrayList in doInBackground() function

//Create service handler class instance
ServiceHandler sh = new ServiceHandler();

//Url request and response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);

if(jsonStr != null) {
    try {
        JSONObject jsonObj = new JSONObject(jsonStr);

        //Get Json Array Node
        users = jsonObj.getJSONArray(TAG_USERS);

        // Looping through all data
        for(int i = 0; i < users.length(); i++) {
            JSONObject u = users.getJSONObject(i);

            String category_id = u.getString(TAG_CATEGORY_ID);
            String user_email = u.getString(TAG_USER_EMAIL);

            // Temporary HashMap for single data
            HashMap<String, String> user = new HashMap<String, String>();

            // Adding each child node to Hashmap key -> value
            user.put(TAG_CATEGORY_ID, category_id);
            user.put(TAG_USER_EMAIL, user_email);

            //Adding user to userList
            userList.add(user);
        }
    }catch (JSONException e) {
        e.printStackTrace();
    }
} else {
    Log.e("ServiceHandler", "Couldn't get any data from url");
}

现在你必须存储在用户列表数据。你可以用你的用户列表,但是你想要的。为了获得在列表中CATEGORY_ID字段,你可以使用这个语法

Now you have your data stored in your userList. You can use your userList however you want. To get the category_id field in the list you can use this syntax

// i being the counter of your loop
String catId = userList.get(i).get("category_id");
if(catId == 3) {
    ... your code
}

这篇关于如何从Json的android系统中过滤数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆