[英]JSONObject cannot be converted to JSONArray for MapBox
2020-02-04 13:35:32.521 32631-32752/com.box.rad100 W/System.err: org.json.JSONException: Value {"id":1,"company":null,"store_desc":"","store_name":"Позиция ЗРК","category_id":2,"latitude":"59.8713584","longitude":"29.8725824","telephone":"Нулевая","keywords":"Нулевое","photo_url":"http:\/\/trikeenan.com\/assets\/starbucks-after-front2.jpg"} of type org.json.JSONObject cannot be converted to JSONArray
2020-02-04 13:35:32.521 32631-32752/com.box.rad100 W/System.err: at org.json.JSON.typeMismatch(JSON.java:112)
请告诉我我的错误是什么。 我不太懂英语,所以请解释一下多么愚蠢,非常感谢。 我的数据.json
{
"id":1,
"store_name":"\u041f\u043e\u0437\u0438\u0446\u0438\u044f \u0417\u0420\u041a",
"category_id":2,
"latitude":"59.8713584",
"longitude":"29.8725824"
}
还有我的代码
public String getJSONFromAssets(Context context) {
String json = null;
try {
InputStream inputData = context.getAssets().open("data.json");
Log.e("100rad", ":"+inputData);
int size = inputData.available();
byte[] buffer = new byte[size];
inputData.read(buffer);
inputData.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
private class AsyncTaskGetMareker extends AsyncTask<String , String, JSONArray> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected JSONArray doInBackground(String... strings) {
String stationsJsonString = getJSONFromAssets(MainActivity.this);
try {
JSONArray stationsJsonArray = new JSONArray(stationsJsonString);
return stationsJsonArray;
} catch (JSONException e) {
e.printStackTrace();
}
//This will only happen if an exception is thrown above:
return null;
}
protected void onPostExecute (JSONArray result){
if (result !=null){
for (int i =0; i <result.length(); i++){
JSONObject jsonObject= null;
try {
jsonObject= result.getJSONObject(i);
String name=jsonObject.getString("store_name");
String lat=jsonObject.getString("latitude");
String lang=jsonObject.getString("longitude");
drawMarker(new LatLng(Double.parseDouble(lat),
Double.parseDouble(lang)), name);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
}
我正在尝试读取一个 json 文件并将其中的数据加载到卡上,谁能在我的 json 示例中解释如何执行此操作? 我为此使用 mapbox 服务
您尝试检索的 JSON 是JSONObject
,而不是JSONArray
,请尝试以下操作:
private class AsyncTaskGetMareker extends AsyncTask<String , String, JSONObject> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected JSONObject doInBackground(String... strings) {
String stationsJsonString = getJSONFromAssets(LoginActivity.this);
try {
JSONObject stationsJsonArray = new JSONObject(stationsJsonString);
return stationsJsonArray;
} catch (JSONException e) {
e.printStackTrace();
}
//This will only happen if an exception is thrown above:
return null;
}
protected void onPostExecute (JSONObject result){
if (result !=null){
for (int i =0; i <result.length(); i++){
JSONObject jsonObject= null;
try {
jsonObject= result;
String name=jsonObject.getString("store_name");
String lat=jsonObject.getString("latitude");
String lang=jsonObject.getString("longitude");
drawMarker(new LatLng(Double.parseDouble(lat),
Double.parseDouble(lang)), name);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
}
希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.