从android中获取json请求的值

时间:2014-07-15 12:13:57

标签: java android json arrays

我需要获取JSON请求并将其设置在以下设计上。

Groupname1

  

Staff1

     

Staff2

Groupname2

  

Staff3

这是我的json回复:

 {"Response":   [{"complete_count":"11","assigned_count":15,"checked_count":"2","staff_firstname":"krish","staff_emp_code":"mmch23 y","staff_id":"29","group_name":"Technical- 1ty"},{"complete_count":"0","assigned_count":3,"checked_count":"0","staff_firstname":"Ravi","staff_emp_code":"MMCH050","staff_id":"38","group_name":"Technical- 1ty"},{"complete_count":"3","assigned_count":4,"checked_count":"0","staff_firstname":"mercy","staff_emp_code":"mmcho56","staff_id":"78","group_name":"Technical- 1ty"},{"complete_count":"0","assigned_count":1,"checked_count":"0","staff_firstname":"Sam ji","staff_emp_code":"Et","staff_id":"71","group_name":"test group"}],"success":1} 

这是我的代码:

class My_WIReports extends AsyncTask<String, String, String> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        showDialog(DIALOG_LOADING);
    }
    protected String doInBackground(String... args) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        json = jParser.makeHttpRequest(url_weekly_list_individual, "POST", params);
        Log.d("My Weekly Individual Reports: ", json.toString());
        try {
             success = json.getInt(TAG_SUCCESS);
            if (success == 1) {

                JSONObject jsonObj = new JSONObject(json.toString());
                 groups = jsonObj.getJSONArray(TAG_STAFFS);
                for (int i = 0; i < groups.length(); i++) {
                    JSONObject c = groups.getJSONObject(i);
                    String groupname = c.getString(TAG_GROUPNAME);
                    String Staff_Complete_count = c.getString(TAG_STAFF_COMPC);
                    String Assig_Count = c.getString(TAG_STAFF_ASSC);
                    String Staff_F_name = c.getString(TAG_STAFF_NAME);
                    String Staff_Emp_code = c.getString(TAG_STAFF_EMPC);
                    String Staff_id = c.getString(TAG_STAFF_ID);
                    String  track_no = String.valueOf(i + 1);
                    map = new HashMap<String, String>();
                    map.put(TAG_GROUPNAME, groupname);
                    map.put(TAG_STAFF_COMPC, Staff_Complete_count);
                    map.put(TAG_STAFF_ASSC, Assig_Count);
                    map.put(TAG_STAFF_NAME, Staff_F_name);
                    map.put(TAG_STAFF_EMPC, Staff_Emp_code);
                    map.put(TAG_STAFF_ID, Staff_id);
                    map.put(TAG_STAFF_NO, track_no);
                    groupList.add(map);

                }
             } else {
                groupList.clear();
                    }
        } catch (JSONException e) {
            e.printStackTrace();
            }
        return json.toString();
    }

这里得到的结果如下:

Technical- 1ty                  -(group_name - 1)
krish                             -(staff_firstname - 1)
Technical- 1ty                  -(group_name  - 1)
Ravi                               -(staff_firstname - 2)
Technical- 1ty                  -(group_name - 1)
Mercy                              -(staff_firstname - 3)
test group                      -(group_name - 2)
sam Ji                              - (staff_firstname -1)

但我需要得到如下结果:

Technical- 1ty                    -(group_name - 1)
krish                               -(staff_firstname - 1)
Ravi                                -(staff_firstname - 2)
Mercy                               -(staff_firstname - 3)
test group                        -(group_name -2) 
sam Ji                              -(staff_firstname - 1)

请给我一个解决方案吗?

如何删除重复的group_name值?

2 个答案:

答案 0 :(得分:0)

my point is:
1.get the values **from service to store vector or array** wat ever your wish...
2.get the **purticular value(groupname) from vector Using position..**
3.Eg:vector size 4...
3.In Adapter you should set 
**below code:**
for(int k=0;k<listvectorlocalsample.size();k++)
{
pojoitems=new SAmplepojo();
pojoitems=listvectorlocalsample.get(position);
String s1,s2l;
s1=pojoitems.getGroup_name().toString();
pojoss=listvectorlocalsample.get(positon+1);
s2=pojoitems.getGroup_name().toString();
if(s1.equalsIgnoreCase(s2l))
{
TextView.INVISIBLE;
}
}

答案 1 :(得分:0)

因此,如果我理解正确,您希望打印一个组名,例如。 “Technical-1ty”然后打印该组的所有成员。

您的问题中没有包含输出代码,因此我只能假设您正在执行以下操作:

for(HashMap<String,String> map : grouplist){
    print map.get(TAG_GROUPNAME);
    print map.get(TAG_STAFF_NAME);
}

很明显,它会打印一个工作人员的组名,然后是他的名字。 你想要的是:

1) Store data of each staff member into a class called staff, instead of a HashMap.
2) Add each staff member to an ArrayList, say staff_members.
3) Sort staff_members by group name field of staff class.
4) Then with your ArrayList<Staff>staff_members (sorted by groupname )you can do the following:

String current_group="";
for(Staff staff_member: staff_members){
    if(!current_group.equals(staff_member.getGroupName()){
        current_group=staff_member.getGroupName();
        System.out.println(current_group);
    }
    System.out.println(staff_member.getFirstName());
}

我理解这对于一个简单的工作来说似乎有很多工作,但是对于诸如你的工作人员之类的东西而不仅仅是一个hashmap而言,正确地使用Classes是一个很好的编码实践。此外,如果您愿意,可以为您的staff_worker类创建一个构造函数,在该类中传递JSONObject,它将为对象分配适当的值。这将有助于为您的代码提供整洁和专业的外观,而不是在主代码中进行如此多的map.put()调用。