android: JSONArray 类型中的 getJSONObject(int) 方法不适用于参数 (String) [英] android: The method getJSONObject(int) in the type JSONArray is not applicable for the arguments (String)

查看:31
本文介绍了android: JSONArray 类型中的 getJSONObject(int) 方法不适用于参数 (String)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有发现我的错误.Eclipse 告诉我:JSONArray 类型中的方法 getJSONObject(int) 不适用于参数 (String)我想访问:匹配数据 -> 目标 -> 目标 ->goal_getter_nameEclipse 错误在这一行:`JSONObject goal = openbuli.getJSONObject("goal");我认为变量 openbuli 是错误的?!我必须如何修复它?

I do not find my error. Eclipse tell me: The method getJSONObject(int) in the type JSONArray is not applicable for the arguments (String) I want to access: matchdata -> goals -> goal -> goal_getter_name The eclipse error is on this line: `JSONObject goal = openbuli.getJSONObject("goal"); I think the variable openbuli ist wrong?! How I have to fix it?

这是我的代码:

String result = "";
SONObject jArray = null;

jArray = new JSONObject(result);

                JSONObject json = (JSONObject) jArray;                 
                JSONArray openbuli = json.getJSONArray("matchdata");

                JSONObject goal = openbuli.getJSONObject("goal");
                JSONArray goals = goal.getJSONArray("goals");          

                 // loop array
                for(int i=0;i<goals.length();i++) {    
                                JSONObject e = goals.getJSONObject(i);
                                Log.e("Name", e.getString("goal_getter_name").toString());
                        }

这里是json数据:

$json (
|    matchdata => Array (9)
|    (
|    |    ['0'] (
|    |    |    league_saison =  "2013"
|    |    |    match_results (
|    |    |    |    match_result => Array (2)
|    |    |    |    (
|    |    |    |    |    ['0'] (
|    |    |    |    |    |    result_name =  "Endergebnis"
|    |    |    |    |    |    result_order_id =  "1"
|    |    |    |    |    |    points_team1 =  "3"
|    |    |    |    |    |    result_type_name =  "nach 90 Minuten"
|    |    |    |    |    |    points_team2 =  "1"
|    |    |    |    |    |    result_type_id =  "2"
|    |    |    |    |    )
|    |    |    |    )
|    |    |    )
|    |    |    goals (
|    |    |    |    goal => Array (4)
|    |    |    |    (
|    |    |    |    |    ['0'] (
|    |    |    |    |    |    goal_match_minute =  "16"
|    |    |    |    |    |    goal_getter_id =  "5112"
|    |    |    |    |    |    goal_id =  "21118"
|    |    |    |    |    |    goal_getter_name =  "Mario Mandzukic"
|    |    |    |    |    |    goal_mach_id =  "23711"
|    |    |    |    |    |    goal_penalty =  FALSE
|    |    |    |    |    |    goal_score_team1 =  "2"
|    |    |    |    |    |    goal_own_goal =  FALSE
|    |    |    |    |    |    goal_score_team2 =  "0"
|    |    |    |    |    |    goal_overtime =  FALSE
|    |    |    |    |    )
|    |    |    |    )
|    |    |    )
|    |    )

推荐答案

您需要将 openbuli.getJSONObject("goal"); 更改为 openbuli.getJSONObject(0);
并添加 getJSONObject("goals") 以从数组中的第一个位置获取对象目标

you need to change openbuli.getJSONObject("goal"); to openbuli.getJSONObject(0);
and add getJSONObject("goals") to get the object goals from the first place in the array

String result = "";
SONObject jArray = null;

jArray = new JSONObject(result);

                JSONObject json = (JSONObject) jArray;                 
                JSONArray openbuli = json.getJSONArray("matchdata");

                JSONObject goal = openbuli.getJSONObject(0);
                JSONArray goals = goal.getJSONObject("goals").getJSONArray("goal");          

                 // loop array
                for(int i=0;i<goals.length();i++) {    
                                JSONObject e = goals.getJSONObject(i);
                                Log.e("Name", e.getString("goal_getter_name").toString());
                        }

这篇关于android: JSONArray 类型中的 getJSONObject(int) 方法不适用于参数 (String)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆