从URL数组中获取数组

时间:2018-12-19 14:39:48

标签: java android arrays json

我正在尝试从JSON数组中提取“ stops”数组,但它包含在另一个名为“ results”的数组中。如何获取“停靠点”并将其放入JSON数组?

已在代码的上方声明了变量(未显示)。还有渔获物(未显示)。

代码给我的错误是:

$(function(){
    $('.panel-group a').click(function() {
        $(this).parent().find('#hasclass').find('.rechter').removeClass('TURN');
        $(this).parent().find('#hasclass').find('.linker').removeClass('TURNN');
    });
});

我需要查看的代码部分是:

2018-12-19 14:31:38.892 6650-6787/ie.[college].student.[studentid].dublinbuses E/StopIdResultsActivity: An error occurred! Error: No value for stops.

示例: JSONXML

protected Void doInBackground(Void... arg0) {
            ...
            ...
                try {

                    origin = "";
                    destination = "";
                    route_result = "";

                    JSONObject jsonObj = new JSONObject(routeid_jsonStr);

                    ...

                    }
                    else {
                    JSONArray results = jsonObj.getJSONArray("results");

                     //looping through All Contacts
                    for (int i = 0; i < results.length(); i++) {

                        JSONObject r = results.getJSONObject(i);
                        JSONArray stops = jsonObj.getJSONArray("stops");
                        for (int j = 0; j < stops.length(); j++) {

                            JSONObject s = stops.getJSONObject(j);

                            stopid = r.getString("stopid");
                            shortname = r.getString("shortname");
                            shortnamelocalized = r.getString("shortnamelocalized");

                            HashMap<String, String> stop = new HashMap<>();

                            // adding each child node to HashMap key => value
                            stop.put("stopid", stopid);
                            stop.put("shortname", shortname);
                            stop.put("shortnamelocalized", shortnamelocalized );

                            resultList.add(stop);
                        }
                    }
                }

2 个答案:

答案 0 :(得分:1)

您似乎使用了两个不同的变量

JSONObject s = stops.getJSONObject(j);

stopid = r.getString("stopid");
shortname = r.getString("shortname");

您将json stop数据存储在s中,但会从r中读取,因此应该是

JSONObject s = stops.getJSONObject(j);

stopid = s.getString("stopid");
shortname = s.getString("shortname");

答案 1 :(得分:1)

请查看以下几行:

JSONObject r = results.getJSONObject(i);
JSONArray stops = jsonObj.getJSONArray("stops");

我认为应该改为:

JSONObject r = results.getJSONObject(i);
JSONArray stops = r.getJSONArray("stops");

在这里,您还混合了各种变量:

stopid = r.getString("stopid");
shortname = r.getString("shortname");
shortnamelocalized = r.getString("shortnamelocalized");

应该是:

stopid = s.getString("stopid");
shortname = s.getString("shortname");
shortnamelocalized = s.getString("shortnamelocalized");