java - MapBox 无法将 JSONObject 转换为 JSONArray

标签 java json

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)

请告诉我我的错误是什么。 我不太懂英文,所以请解释一下如何愚蠢,非常感谢。 我的data.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 服务

My json screen

最佳答案

您尝试检索的 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();
                    }
                }
            }
        }
    }

希望这有帮助

关于java - MapBox 无法将 JSONObject 转换为 JSONArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60055622/

相关文章:

java - 在我创建 JPanel 的同一部分绘制网格线?

java - 数9,为什么小于9?

javascript - PostMan 测试脚本 : Inspecting contents of response JSON

php - 将数组添加到 MySQL Fetch

javascript - 带有解析时间或时间戳的 JSON 到 amCharts

json - 密码查询JSON格式的结果

Java网络服务: User defined meta-data

java - Eclipse 中的 BigInteger 有限

json - 使用 Swift 读取 JSON 文件

java - 如何在 Seam 2 中实现 resourcebundle (messages.properties) 的热重载