Php 在android中,如何获得排名第一的变量是第一个对象中的object,第二个对象中有数组
这是我的APIPhp 在android中,如何获得排名第一的变量是第一个对象中的object,第二个对象中有数组,php,android,mysql,arrays,json,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 ranki
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.
},
像这样试试
首先创建arraylist以存储yout数据
ArrayList<List_items> list_items_arr = new ArrayList<List_items>();
从服务器获取响应并解析它
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);
}
JSONObject jsono=newjsonobject(json);
JSONArray items=jsono.getJSONArray(“排名”);
对于(inti=0;i您可以使用
首先,我们得到了团队阵列
JSONArray teamArray=result.getJSONArray("Team");
然后我们迭代各个团队
for(int i=0;i<teamArray.length();i++)
{
Object ranking=teamArry.getJSONObject(i).get("ranking");
JSONObject tt20=saris.getJSONObject(“排名”);t20=tt20.getString(“内容”);JSONArray rank=saris.getJSONArray(“排名”);JSONObject t201=rank.getJSONObject(0);t20=t201.getString(“内容”);JSONObject odi=rank.getJSONObject(1);odi=odi.getString(“内容”);JSONObject test=rank.getJSONObject(2);content=test.getString(“内容”);adapter_data.add(new TeamModel(TeamId,TeamName,ShortName,content,t20,Odi));}更新您的格式,因为在第一个对象中,排名为jsonobject,在第二个对象中排名为arrayaove,这是我获取json对象排名的Java代码:{mtype:“t20”,ranker:“icc”,trend:“stable”,content:“14”},排名:[{mtype:“Test”,ranker:“icc”,趋势:“稳定”,内容:“1”},{mtype:“ODI”,ranker:“icc”,趋势:“稳定”,内容:“1”},{mtype:“T20”,ranker:“icc”,趋势:“稳定”,内容:“6”}],我只想要内容…项目id是什么…我想要t20变量中的t20内容,测试变量中的测试内容,Odi变量中的Odi内容..使用你自己的变量名,我只说了一个例子,但你看不到我的json代码,我已经这样做了…org.json.JSONException:Value{“content”:“14”,“ranker”:“icc”,“trend”:“stable”,“mtype”:“T20”}在org.json.JSONObject类型的排名中无法转换为JSONArray。我得到的错误是如果(JSONObject的排名实例){JSONObject tt20=saris.getJSONObject(“排名”);T20=tt20.getString(“内容”);}否则如果(JSONArray的排名实例){JSONArray rank=saris.getJSONArray(“排名”);JSONObject t201=rank.getJSONObject(0);t20=t201.getString(“内容”);JSONObject odi=rank.getJSONObject(1);odi=odi.getString(“内容”);JSONObject test=rank.getJSONObject(2);content=test.getString(“内容”);}我得到这样的数据…谢谢你的帮助…当你得到数组时,你可以得到第一个像这样的排名:[{mtype:“Test”,ranker:“icc”,trend:“stable”,content:“1”},{mtype:“ODI”,ranker:“icc”,trend:“stable”,content:“6”],如果您想要数组的第一个秩,在本例中为ranker:“icc”,您可以像这样得到arrayRankig.getJSONObject(0).getString(“ranker”);使用cero,您可以得到数组中的第一个Jsonobject,并从该对象中得到ranker的字符串值,这就是您要问的吗?
for(int i=0;i<teamArray.length();i++)
{
Object ranking=teamArry.getJSONObject(i).get("ranking");
if(ranking instanceof JSONObject)
{
//get the data
}
else if( ranking instanceof JSONArray)
{
//get the data
}
}