如何获得排名第一变量是第一个对象中的对象,而android中的第二个对象中有数组

时间:2016-05-06 10:59:31

标签: php android mysql arrays json

这是我的API。

   results: {   this is my api.
    Team: [
    {
     first object.
    TeamId: {   team id.
    status: "1",
    content: "21"  this is done
    },
    TeamName: "United Arab Emirates",
    ShortName: "UAE"
    Ranking: {   how to get ranking this is object but scroll down this is      array. how to get value of content by both section .
    content: "14"    get it
    },
    },
    {
     //second object.
    TeamId: {   teamid done
    status: "1",
    content: "1188"
    },
    TeamName: "Afghanistan",
    ShortName: "AFG",
    Ranking: [   this is array how to read it in android.
    {
    content: "10"   get it
    },
    {
    content: "9"   get it
    }
    ],     add ranking in different variable like a=10,b=9. 
    },

2 个答案:

答案 0 :(得分:0)

试试这个

首先创建一个arraylist来存储你的数据

 ArrayList<List_items> list_items_arr = new ArrayList<List_items>();

为Lisr_items创建类

  public class List_items{
   String item_id,item_name;
public void setItem_id(String item_id) {
        this.item_id = item_id;
    }

public void setItem_name(String item_name) {
    this.item_name = item_name;
   }
}

从服务器获取响应并解析它

JSONObject jsono = new JSONObject(json);

JSONArray items = jsono.getJSONArray("Ranking");

  for(int i=0;i<items.length();i++){
         JSONObject list_obj=items.getJSONObject(i);
            List_items list_items = new List_items();
            list_items.setItem_name(list_obj.getString("mtype"));
            list_items.setItem_id(list_obj.getString("trend"));
     list_items_arr .add(list_items);
 }

答案 1 :(得分:0)

你可以用这个

我们得到Team数组

JSONArray teamArray=result.getJSONArray("Team");
然后我们迭代团队

for(int i=0;i<teamArray.length();i++)
{
    Object ranking=teamArry.getJSONObject(i).get("ranking");

取决于其排名数组或仅一个JSONObject排名

    if(ranking instanceof JSONObject)
    {
      //get the data
    }
    else if( ranking instanceof JSONArray)
    {
      //get the data
    }
}