org.json.JSONException:类型为java.lang.Double的0的值10.07526无法转换为JSONObject

时间:2016-04-12 19:22:27

标签: android json coordinates

我想从JSON解析坐标。我的问题是:org.json.JSONException:类型为java.lang.Double的0的值10.07526无法转换为JSONObject。

感谢您的帮助。

我的JSON对象:

"placemarks": [{
            "address": "xxxstreet 170, 43023 City",
            "coordinates": [
                10.07526,
                53.59301,
                0
            ],
            "engineType": "CE",
            "exterior": "UNACCEPTABLE",
            "fuel": 42,
            "interior": "UNACCEPTABLE",
            "name": "xxx",
            "vin": "xxx"
        },

我的活动:

public class MainActivity extends Activity {

ListView lvCars;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    DownloadTask task = new DownloadTask();
    task.execute("http://example.com");

    lvCars = (ListView) findViewById(R.id.listViewCars);

}

public class DownloadTask extends AsyncTask <String, String, List<JSONModels> >{

    @Override
    protected List<JSONModels> doInBackground(String... params) {

        String result = "";
        URL url;
        HttpURLConnection urlConnection = null;

        try {

            url = new URL(params[0]);

            urlConnection = (HttpURLConnection) url.openConnection();

            //holds the Input of data
            InputStream inputStream = urlConnection.getInputStream();
            InputStreamReader reader = new InputStreamReader(inputStream);

            int data = reader.read();

            while (data != -1) {

                char current = (char) data;

                result += current;

                data = reader.read();
            }


        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        List<JSONModels> jsonModelsList = null;
        try {
            JSONObject jsonObject = new JSONObject(result);
            String placemarks = jsonObject.getString("placemarks");
            JSONArray jsonArray = new JSONArray(placemarks);

            jsonModelsList = new ArrayList<>();

            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonPart = jsonArray.getJSONObject(i);
                JSONModels jsonModels = new JSONModels();
                jsonModels.setName(jsonPart.getString("name"));
                jsonModels.setAddress(jsonPart.getString("address"));
                jsonModels.setExterior(jsonPart.getString("exterior"));
                jsonModels.setInterior(jsonPart.getString("interior"));
                jsonModels.setFuel(jsonPart.getInt("fuel"));

                List<JSONModels.coordinates> coordinatesList = new ArrayList<>();
                for (int j = 0; j < jsonPart.getJSONArray("coordinates").length(); j++) {
                    JSONModels.coordinates coordinates = new JSONModels.coordinates();
                    coordinates.setCoordinates(jsonPart.getJSONArray("coordinates").getJSONObject(j).getDouble("coordinates"));
                    coordinatesList.add(coordinates);
                }
                jsonModels.setCoordinateslist(coordinatesList);
                jsonModelsList.add(jsonModels);

            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonModelsList;
    }

    @Override
    protected void onPostExecute(List<JSONModels> result) {
        super.onPostExecute(result);

        CarAdapter adapter = new CarAdapter(getApplicationContext(), R.layout.row, result);
        lvCars.setAdapter(adapter);

    }
}

public class CarAdapter extends ArrayAdapter{

    private List<JSONModels> carModelList;
    private int resource;
    private LayoutInflater inflater;
    public CarAdapter(Context context, int resource, List<JSONModels> objects) {
        super(context, resource, objects);
        carModelList = objects;
        this.resource = resource;
        inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if(convertView == null){
           convertView = inflater.inflate(resource, null);
        }

        ImageView ivIcon;
        TextView tvAddress;
        TextView tvName;
        TextView tvExterior;
        TextView tvInterior;
        TextView tvFuel;

        ivIcon = (ImageView) convertView.findViewById(R.id.ivIcon);
        tvAddress = (TextView) convertView.findViewById(R.id.tvAddress);
        tvName = (TextView) convertView.findViewById(R.id.tvName);
        tvExterior = (TextView) convertView.findViewById(R.id.tvExterior);
        tvInterior = (TextView) convertView.findViewById(R.id.tvInterior);
        tvFuel = (TextView) convertView.findViewById(R.id.tvFuel);

        tvAddress.setText(carModelList.get(position).getAddress());
        tvName.setText(carModelList.get(position).getName());
        tvExterior.setText("Exterior: "+ carModelList.get(position).getExterior());
        tvInterior.setText("Interior: " + carModelList.get(position).getInterior());
        tvFuel.setText("Fuel: " + carModelList.get(position).getFuel());

        return convertView;
    }
}
}

3 个答案:

答案 0 :(得分:1)

试试这样:

try {
        JSONObject jsonObject = new JSONObject("");
        JSONArray placemarks = jsonObject.optJSONArray("placemarks");

        for(int i = 0; i< placemarks.length(); i++){
            JSONObject marker = placemarks.getJSONObject(i);
            JSONArray coordinates = marker.optJSONArray("coordinates");
            double lat = coordinates.optDouble(0);
            double lng = coordinates.optDouble(1);
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }

答案 1 :(得分:0)

因为这不是你应该如何解析它,你应该这样做

jsonPart.getJSONArray("coordinates").getDouble(j)

答案 2 :(得分:0)

处理json的另一种方法是将Gson与POJO类一起使用。

1)查看该网站:Jsonschema2pojo

2)添加gson dependecy:compile 'com.google.code.gson:gson:2.3.1'

3)将您的示例json格式粘贴到网站并创建POJO类。

4)在你的java代码中:

Gson gson = new GsonBuilder().create();

YourPojoClass pojo=new YourPojoClass();

try {
     pojo=gson.fromJson(yourjsonresponse.toString(),YourPojoClass.class);
    } catch (IOException e) {// handle io errors}