我一直在尝试所有给出JSON响应的内容,并在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"
}]
}]
基本上我想在Recyclerview中显示所有属于团队的人。即
"teamName":"iOS","teamName" : "Android","teamName" : "Android","Web" : "Design"
尝试了这个:
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);
}
}
现在我为每个成员都命名相同。:(
答案 0 :(得分:2)
几乎没有变化:你不需要第二次循环,不需要检查物体的长度。在内循环中重置Model
。
试试这个
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();
}
}
答案 1 :(得分:0)
跳过开始循环的第一个元素1.
我也改变了你的循环以使用正确的变量,让我知道它是否有效:
@Override
public void onResponse(JSONArray response) {
Log.d("Theo", response.toString());
for(int i = 1;i<response.length();i++){
// i = 1 skips the first element
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(j);
m.setFirstName(teamObject.getString("firstName"));
m.setLastName(teamObject.getString("lastName"));
m.setRole(teamObject.getString("role"));
modelArrayList.add(m);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}