gpt4 book ai didi

android - org.json.JSONException : Value 10. 07526 at 0 of type java.lang.Double cannot be converted to JSONObject

转载 作者:行者123 更新时间:2023-11-30 01:24:33 25 4
gpt4 key购买 nike

我想从 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"
},

我的 Activity :

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;
}
}
}

最佳答案

像这样尝试:

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();
}

关于android - org.json.JSONException : Value 10. 07526 at 0 of type java.lang.Double cannot be converted to JSONObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36582247/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com