Theo Theo - 5 months ago 12
Android Question

Messy JSON response,gives my trouble

I have been trying all everything that given JSON response,and use it in Android.

[{
"id": "001",
"firstName": "Mark",
"lastName": "Mason",
"role": "CEO",
"profileImageURL": "http://developers.mub.lu/resources/profilePlaceholder.png"
}, {
"teamName": "iOS",
"members": [{
"id": "002",
"firstName": "Olly",
"lastName": "Berry",
"role": "iOS Team Lead",
"profileImageURL": "http://developers.mub.lu/resources/profilePlaceholder.png",
"teamLead": true
}, {
"id": "003",
"firstName": "James",
"lastName": "Frost",
"role": "iOS Developer",
"profileImageURL": "http://developers.mub.lu/resources/profilePlaceholder.png"
}, {
"id": "004",
"firstName": "Liam",
"lastName": "Nichols",
"role": "iOS Developer",
"profileImageURL": "http://developers.mub.lu/resources/profilePlaceholder.png"
}, {
"id": "005",
"firstName": "Chris",
"lastName": "Watson",
"role": "iOS Developer",
"profileImageURL": "http://developers.mub.lu/resources/profilePlaceholder.png"
}, {
"id": "006",
"firstName": "Richard",
"lastName": "Turton",
"role": "iOS Developer",
"profileImageURL": "http://developers.mub.lu/resources/profilePlaceholder.png"
}, {
"id": "007",
"firstName": "Matt",
"lastName": "Colliss",
"role": "iOS Developer",
"profileImageURL": "http://developers.mub.lu/resources/profilePlaceholder.png"
}, {
"id": "008",
"firstName": "David",
"lastName": "Gibson",
"role": "iOS Developer",
"profileImageURL": "http://developers.mub.lu/resources/profilePlaceholder.png"
}, {
"id": "009",
"firstName": "Tom",
"lastName": "Guy",
"role": "iOS Developer",
"profileImageURL": "http://developers.mub.lu/resources/profilePlaceholder.png"
}, {
"id": "010",
"firstName": "Rich",
"lastName": "Hodgkins",
"role": "iOS Developer",
"profileImageURL": "http://developers.mub.lu/resources/profilePlaceholder.png"
}]
}, {
"teamName": "Android",
"members": [{
"id": "011",
"firstName": "David",
"lastName": "Branton",
"role": "Android Team Lead",
"profileImageURL": "http://developers.mub.lu/resources/profilePlaceholder.png",
"teamLead": true
}, {
"id": "012",
"firstName": "Dre",
"lastName": "Pilipczuk",
"role": "Android Developer",
"profileImageURL": "http://developers.mub.lu/resources/profilePlaceholder.png"
}, {
"id": "013",
"firstName": "Ray",
"lastName": "Britton",
"role": "Android Developer",
"profileImageURL": "http://developers.mub.lu/resources/profilePlaceholder.png"
}, {
"id": "014",
"firstName": "Charly",
"lastName": "Murillo",
"role": "Android Developer",
"profileImageURL": "http://developers.mub.lu/resources/profilePlaceholder.png"
}]
}, {
"teamName": "Web",
"members": [{
"id": "015",
"firstName": "Ryan",
"lastName": "French",
"role": "Web Team Lead",
"profileImageURL": "http://developers.mub.lu/resources/profilePlaceholder.png",
"teamLead": true
}, {
"id": "016",
"firstName": "James",
"lastName": "Ward",
"role": "Web Developer",
"profileImageURL": "http://developers.mub.lu/resources/profilePlaceholder.png"
}, {
"id": "018",
"firstName": "Adam",
"lastName": "Smith",
"role": "Web Developer",
"profileImageURL": "http://developers.mub.lu/resources/profilePlaceholder.png"
}, {
"id": "019",
"firstName": "Leonard",
"lastName": "Da Costa",
"role": "Web Developer",
"profileImageURL": "http://developers.mub.lu/resources/profilePlaceholder.png"
}]
}, {
"teamName": "Design",
"members": [{
"id": "020",
"firstName": "Hannah",
"lastName": "Tempest",
"role": "Design Team Lead",
"profileImageURL": "http://developers.mub.lu/resources/profilePlaceholder.png",
"teamLead": true
}, {
"id": "021",
"firstName": "Ellis",
"lastName": "Reed",
"role": "Designer",
"profileImageURL": "http://developers.mub.lu/resources/profilePlaceholder.png"
}, {
"id": "022",
"firstName": "Pete",
"lastName": "Horsham",
"role": "Designer",
"profileImageURL": "http://developers.mub.lu/resources/profilePlaceholder.png"
}, {
"id": "023",
"firstName": "Hemel",
"lastName": "Dave",
"role": "Designer",
"profileImageURL": "http://developers.mub.lu/resources/profilePlaceholder.png"
}, {
"id": "024",
"firstName": "Hannah",
"lastName": "Corke",
"role": "Designer",
"profileImageURL": "http://developers.mub.lu/resources/profilePlaceholder.png"
}]
}]


Basically I want to display in a recyclerview,all people who belong in a team. ie

"teamName":"iOS","teamName" : "Android","teamName" : "Android","Web" : "Design"


It tried this:

package testing.theo.androidtestproject.fragment;



public class TestingFragment extends Fragment {
public static final String IMAGE_URL =
"http://developers.mub.lu/resources/";
private ArrayList<Model> modelArrayList;
private RecyclerView mRecyclerView;
private MyAdapter myAdapter;

public TestingFragment() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_testing, container, false);

modelArrayList = new ArrayList<>();

mRecyclerView = (RecyclerView)v.findViewById(R.id.recycler_view);

final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(linearLayoutManager);

myAdapter = new MyAdapter(getActivity(),modelArrayList);
mRecyclerView.setAdapter(myAdapter);

showTestData();
return v;


}

public void showTestData()
{



// Request a string response from the provided URL.
final JsonArrayRequest jsObjRequest = new JsonArrayRequest(Request.Method.GET, URLClass.URL, new Response.Listener<JSONArray>() {

@Override
public void onResponse(JSONArray response) {
Log.d("Theo", response.toString());

for(int i = 1;i<response.length();i++){


try {


JSONObject jsonObject = response.getJSONObject(i);


JSONArray teamMembersArray =
jsonObject.getJSONArray("members");
for(int j=0;j<teamMembersArray.length();j++){
Model m = new Model();

JSONObject teamObject = teamMembersArray.getJSONObject(i);
m.setFirstName(teamObject.getString("firstName"));
m.setLastName(teamObject.getString("lastName"));
m.setRole(teamObject.getString("role"));
modelArrayList.add(m);
}



} catch (JSONException e) {
e.printStackTrace();
}

}


//modelArrayList.add(m);
// Update list by notifying the adapter of changes
myAdapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

//hidePD();
}
});
AppController.getInstance().addToRequestQueue(jsObjRequest);

}

}


Now I get the same name for every member.:(

Answer

Few changes: You don't need second loop, no need to check length of object. Reset Model in inner loop.

Try this

for(int i = 1;i<response.length();i++){ // skipping first object as you said
    try {
        JSONObject jsonObject = response.getJSONObject(i);
        //m.setRole(jsonObject.getString("teamName"));

        JSONArray teamMembersArray = jsonObject.getJSONArray("members");
        for(int j=0;j<teamMembersArray.length();j++){
            JSONObject teamObject = teamMembersArray.getJSONObject(j);
            Model m = new Model(); // reset model in inner loop
            m.setFirstName(teamObject.getString("firstName"));
            m.setLastName(teamObject.getString("lastName"));
            m.setRole(teamObject.getString("role"));
            modelArrayList.add(m);
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }
}
Comments