将数据从android中的JSON数组放入数组

时间:2015-03-10 16:23:13

标签: android arrays json android-asynctask

正确初始化后编辑,但一切正常但我仍然只获得“events”数组中的第一个对象。为什么?

我有一个AsyncTask连接到服务器以下载JSON文件并从那里获取JSON数组。问题是,当我想使用for循环从同一个数组中提取另一个数据时,我得到了一个空指针异常。为什么?我从数组“事件”中提取值“lat”,“lon”,“text”。 以下是我的JSON外观:

{
"current_ts": 1425907330,
"username": "guri",
"events": [
    {
        "id": 16591481,
        "ts": 1425907325,
        "lat": 48.17,
        "lon": 17.13,
        "likes": 5,
        "text": "Poziar na hlavnej stanici",
        "tags": "#poziar #stanica #hori",
        "img": "2002-06-19.jpg"
    },
    {
        "id": 47067411,
        "ts": 1425907100,
        "lat": 48.81,
        "lon": 17.22,
        "likes": 0,
        "text": "V Bille je velky vypredaj",
        "tags": [
            {
                "tag1": "#akcia",
                "tag2": "#akcia"
            }
        ],
        "img": "DSC04934.jpg"
    }
]

这是我的AsyncTask的代码片段:

 @Override
    protected Void doInBackground(Void... params) {

        // Retrieve JSON Objects from the given URL address
        jsonobject = JSONfunctions
                .getJSONfromURL("http://guri.sk/mapped/mapped.json");

        try {
            // Locate the array name in JSON
            event = jsonobject.getJSONArray("events");

            for (int i = 0; i < event.length(); i++) {
                jsonobject = event.getJSONObject(i);
                lat2[i] = jsonobject.getDouble("lat");
                lon2[i] = jsonobject.getDouble("lon");
                text1[i] = jsonobject.getString("text");
                number = i;
                }
            } catch (JSONException e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }

    @Override
    protected void onPostExecute(Void args) {

        // Close the progressdialog
        mProgressDialog.dismiss();
        for (int i = 0; i < number; i++) {
        mMap.addMarker(new MarkerOptions().position(new LatLng(lat2[i], lon2[i])).title(text1[i]));
        }
    }

我如何初始化:

 double[] lat2 = new double[10];
 double[] lon2 = new double[10];
 String[] text1 = new String[10];

2 个答案:

答案 0 :(得分:0)

修改后回答您的问题:

如果你的JSON有两个元素,那么for循环值后面的number将是1,然后onPostExecute for循环将只对数组中的第一个元素执行。所以我认为你应该在for循环前设置数值:

number = event.length();
for (int i = 0; i < event.length(); i++) {
    jsonobject = event.getJSONObject(i);
    lat2[i] = jsonobject.getDouble("lat");
    lon2[i] = jsonobject.getDouble("lon");
    text1[i] = jsonobject.getString("text");
}

答案 1 :(得分:0)

问题在于,您无法出于任何原因直接使用AsyncTask的onPostExecute中的循环。你必须将它包装在一个方法中。以下是有效的代码段:

protected Void doInBackground(Void... params) {

        // Retrieve JSON Objects from the given URL address
        jsonobject = JSONfunctions
                .getJSONfromURL("http://guri.sk/mapped/mapped.json");

        try {
            // Locate the array name in JSON
            event = jsonobject.getJSONArray("events");
            number = event.length();
            for (int i = 0; i < event.length(); i++) {
                jsonobject = event.getJSONObject(i);
                lat2[i] = jsonobject.getDouble("lat");
                lon2[i] = jsonobject.getDouble("lon");
                text1[i] = jsonobject.getString("text");
                //Log.e("TAG","lat2:"+lat2[i]+"lon2:"+lon2[i]);

                }

            } catch (JSONException e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }

    @Override
    protected void onPostExecute(Void args) {

        // Close the progressdialog
        mProgressDialog.dismiss();
        placeMarkers();
    }
}
public void placeMarkers(){
    for (int i = 0; i < number; i++) {
        Log.e("TAG","lat2:"+lat2[i]+"lon2:"+lon2[i]);
        mMap.addMarker(new MarkerOptions().position(new LatLng(lat2[i], lon2[i])).title(text1[i]));
    }
}