为循环赋予更多的价值然后应该(java android)

时间:2015-05-11 07:53:01

标签: java android loops for-loop

我有一个像这样的json数组

[
  {
    "id": "11",
    "notes": "edgbdefgbfffffffffffffffffffff",
    "start_date": "2015-04-28",
    "end_date": "2015-04-29",
    "user_id": "66",
    "status": "1",
    "from_country": "Armenia",
    "from_state": "Aragatsotn",
    "from_city": "Aragatsotn",
    "from_street": "Avan Road",
    "from_apt": "",
    "from_longitude": "44.174652099609375",
    "from_latitude": "40.35805621890491",
    "from_place_id": "ChIJq1tZBgOJakAR79py0zHBCVo",
    "to_country": "Armenia",
    "to_state": "Yerevan",
    "to_city": "Yerevan",
    "to_street": "Tsitsernakaberd Highway",
    "to_apt": "",
    "to_longitude": "44.487762451171875",
    "to_latitude": "40.18097176388719",
    "to_place_id": "ChIJJcq4GKm9akARQboxrm84dHY",
    "creator_first_name": "Poxos1s",
    "creator_last_name": "Poxosyan",
    "creator_paypal_email": null,
    "creator_credit_card": null,
    "creator_phone": "374 99 99 99 99",
    "creator_email": "poxos@gmail.com",
    "creator_file_id": "163",
    "from_country_short": "AM",
    "to_country_short": "AM"
  },
  {
    "id": "10",
    "notes": "rtyerty",
    "start_date": "2015-04-24",
    "end_date": "2015-04-25",
    "user_id": "61",
    "status": "1",
    "from_country": "Armenia",
    "from_state": "Aragatsotn",
    "from_city": "Aragatsotn",
    "from_street": "H20",
    "from_apt": "",
    "from_longitude": "44.219970703125",
    "from_latitude": "40.46157664398329",
    "from_place_id": "ChIJ23CVRId-akARPsgD3Zt77bI",
    "to_country": "Armenia",
    "to_state": "Armavir",
    "to_city": "Armavir",
    "to_street": "M-3 M-3",
    "to_apt": "",
    "to_longitude": "44.3243408203125",
    "to_latitude": "40.204050425113294",
    "to_place_id": "EhBNLTMgTS0zLCBBcm1lbmlh",
    "creator_first_name": "Hovsep",
    "creator_last_name": "Markosyan",
    "creator_paypal_email": null,
    "creator_credit_card": null,
    "creator_phone": "077310128",
    "creator_email": "markosyanhoso@mail.ru",
    "creator_file_id": "263",
    "from_country_short": "AM",
    "to_country_short": "AM"
  },
  {
    "id": "9",
    "notes": "ghjgh",
    "start_date": "2015-04-22",
    "end_date": "2015-04-24",
    "user_id": "66",
    "status": "1",
    "from_country": "Armenia",
    "from_state": "Aragatsotn",
    "from_city": "Aragatsotn",
    "from_street": "Factory Street",
    "from_apt": "",
    "from_longitude": "44.32708740234375",
    "from_latitude": "40.33188951824973",
    "from_place_id": "ChIJF--_mNmEakARLtYmVplqp7c",
    "to_country": "Armenia",
    "to_state": "Armavir",
    "to_city": "Armavir",
    "to_street": "Unnamed Road",
    "to_apt": "",
    "to_longitude": "44.263916015625",
    "to_latitude": "40.19041398364302",
    "to_place_id": "ChIJ01kFw2eTakARVNQCrG2iewg",
    "creator_first_name": "Poxos1s",
    "creator_last_name": "Poxosyan",
    "creator_paypal_email": null,
    "creator_credit_card": null,
    "creator_phone": "374 99 99 99 99",
    "creator_email": "poxos@gmail.com",
    "creator_file_id": "163",
    "from_country_short": "AM",
    "to_country_short": "AM"
  },
  {
    "id": "3",
    "notes": "yky",
    "start_date": "2015-04-23",
    "end_date": "2015-04-30",
    "user_id": "60",
    "status": "1",
    "from_country": "Armenia",
    "from_state": "Yerevan",
    "from_city": "Yerevan",
    "from_street": "3 Agatangeghos Street",
    "from_apt": "",
    "from_longitude": "44.510765075683594",
    "from_latitude": "40.170872380088376",
    "from_place_id": "ChIJ58PEYFi8akARtejTCFF4jOk",
    "to_country": "Armenia",
    "to_state": "Yerevan",
    "to_city": "Yerevan",
    "to_street": "Kond Street",
    "to_apt": "",
    "to_longitude": "44.502010345458984",
    "to_latitude": "40.18215211323218",
    "to_place_id": "ChIJa_8u6AG9akARK2k5uMHdgJ8",
    "creator_first_name": "Miqo",
    "creator_last_name": "Dallaqyan",
    "creator_paypal_email": "ssdfsd@sdfsdf.sda",
    "creator_credit_card": "1234 5678 9123 4564",
    "creator_phone": "12345677",
    "creator_email": "aaa@aa.aa",
    "creator_file_id": "247",
    "from_country_short": "AM",
    "to_country_short": "AM"
  }

 .......................

]

我以这种方式循环遍历数组:

 try {
        JSONArray array = new JSONArray(roadsresponse);
        Log.d("roadsresponse",roadsresponse);
        for (int i = 0; i < array.length(); i++) {
            JSONObject row = array.getJSONObject(i);
            roadstolongitude = row.getString("to_longitude");
            roadstolatitude = row.getString("to_latitude");
            roadsfirstname = row.getString("creator_first_name");
            roadslastname = row.getString("creator_last_name");
            roadsstartdate = row.getString("start_date");
            roadsenddate = row.getString("end_date");
            roadsfromplace = row.getString("from_country");
            roadstoplace = row.getString("to_country");
            roadsfromcity = row.getString("from_city");
            roadstocity = row.getString("to_city");
            roadsnotes = row.getString("notes");
            roadsfromlatitude = row.getString("from_latitude");
            roadsfromlongitude = row.getString("from_longitude");
            double tolongdouble = Double.parseDouble(roadstolongitude);
            double tolatdouble = Double.parseDouble(roadstolatitude);
            MarkerOptions options = new MarkerOptions();
            options.position(new LatLng(tolatdouble, tolongdouble));
            options.title("roads");
            options.rotation(1);
            options.icon(BitmapDescriptorFactory.fromResource(R.drawable.roads));
            options.snippet(roadsfirstname + "/" + roadslastname + "/" + roadsstartdate + "/" + roadsenddate + "/" + roadsfromplace + "/" + roadsfromcity + "/" + roadstoplace + "/" + roadstocity + "/" + roadsnotes + "/" + roadsfromlongitude + "/" + roadsfromlatitude + "/" + roadstolongitude + "/" + roadstolatitude);
            map2.addMarker(options);
            Log.d("toplace",roadstoplace);

您可以看到我正在打印roadstoplace,并且我正在接收此类打印

 05-11 11:37:02.937  13832-13832/com.paqqy D/toplace﹕ Armenia
05-11 11:37:02.938  13832-13832/com.paqqy D/toplace﹕ Armenia
05-11 11:37:02.940  13832-13832/com.paqqy D/toplace﹕ Armenia
05-11 11:37:02.942  13832-13832/com.paqqy D/toplace﹕ Armenia
05-11 11:37:02.944  13832-13832/com.paqqy D/toplace﹕ Armenia
05-11 11:37:02.945  13832-13832/com.paqqy D/toplace﹕ Armenia
05-11 11:37:02.947  13832-13832/com.paqqy D/toplace﹕ Armenia
05-11 11:37:02.949  13832-13832/com.paqqy D/toplace﹕ Armenia
05-11 11:37:02.951  13832-13832/com.paqqy D/toplace﹕ Armenia
05-11 11:37:02.953  13832-13832/com.paqqy D/toplace﹕ null
05-11 11:37:02.955  13832-13832/com.paqqy D/toplace﹕ null

首先,虽然它很正常,但阵列中应该有11个元素,但是当我手动开始计算时,我得指出我有9个元素,最后2个NULL个元素来了从哪儿....那么关于它的问题是什么?我在循环中做错了吗?我应该收到9个元素而不是11个,但我确实收到11个元素,最有趣的是最后2个元素是null ....请帮助我!

0 个答案:

没有答案