【问题标题】:parsing JSON string for an array in Java在 Java 中为数组解析 JSON 字符串
【发布时间】:2018-05-02 21:45:44
【问题描述】:

我正在编写一个从 SQL 数据库中提取 JSON 数据的 Android 应用程序。

在我的异步任务类中,我从 HTTP 请求接收到一个 JSON 字符串,并将其分配给 JSONObject。该字符串包含各种数据,包括我需要的 坐标数组。在那之后,我做obj.getJSONArray("coordinates"); 一旦我这样做,我的应用程序就会抛出一个异常:

org.json.JSONObject cannot be converted to JSONArray in android

我正在按照这篇文章的方法解决我的问题,但没有运气:

org.json.JSONObject cannot be converted to JSONArray in android

我觉得我需要遍历我的 JSON 字符串,直到我看到那个数组。我将在下面发布字符串数据。

protected Void doInBackground(Void... voids) {
            HttpHandler sh = new HttpHandler();

            // Making a request to url and getting response
            String jsonStr = sh.makeServiceCall(address);

            //Log.e(TAG, "Response from url: " + jsonStr);
            if (jsonStr != null) {
                try {

                    JSONObject obj = new JSONObject(jsonStr);


                    JSONArray deviceJsonArray = obj.getJSONArray("coordinates"); //ERROR OCCURS HERE!

                    // looping through All Devices
                    for (int i = 0; i < deviceJsonArray.length(); i++) {
                        JSONObject json = deviceJsonArray.getJSONObject(i);

                        //String DeviceID = c.getString("id");
//                        longitude = json.getDouble("longitude");
//                        latitude =  json.getDouble("latitude");

                        Device device = new Device();
                        device.setName(receivedDeviceName);

                        //Adding the information to the array.
                        coordinatesArray.add(device);
                    }

                    //Log.e(TAG, "arraylist: " + coordinatesArray);
                } catch (final JSONException e) {
                    Log.e(TAG, "Json parsing error: " + e.getMessage());
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(),
                                    "Json parsing error: " + e.getMessage(),
                                    Toast.LENGTH_LONG)
                                    .show();
                        }
                    });

                }
            } else {
                Log.e(TAG, "Couldn't get json from server.");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(),
                                "Couldn't get json from server. Check LogCat for possible errors!",
                                Toast.LENGTH_LONG)
                                .show();
                    }
                });
            }

            return null;
        }

JSON 字符串值:

{"data":{"type":"gps","total_distance":18,"coordinates":[{"latitude":49.169209,"longitude":-122.990952,"time":"12:02:38 AM"},{"latitude":49.16922,"longitude":-122.990959,"time":"2:42:33 PM"},{"latitude":49.1693,"longitude":-122.988098,"time":"5:16:33 PM"},{"latitude":49.170979,"longitude":-122.968239,"time":"5:18:33 PM"},{"latitude":49.174992,"longitude":-122.962502,"time":"5:20:33 PM"},{"latitude":49.17688,"longitude":-122.95768,"time":"5:22:33 PM"},{"latitude":49.182232,"longitude":-122.960297,"time":"5:24:33 PM"},{"latitude":49.18581,"longitude":-122.950813,"time":"5:26:33 PM"},{"latitude":49.188869,"longitude":-122.945969,"time":"5:28:33 PM"},{"latitude":49.197689,"longitude":-122.948502,"time":"5:30:33 PM"},{"latitude":49.20216,"longitude":-122.947418,"time":"5:32:33 PM"},{"latitude":49.2071,"longitude":-122.949593,"time":"5:34:33 PM"},{"latitude":49.213051,"longitude":-122.938522,"time":"5:36:33 PM"},{"latitude":49.215462,"longitude":-122.93399,"time":"5:38:33 PM"},{"latitude":49.216381,"longitude":-122.932297,"time":"5:40:33 PM"},{"latitude":49.218769,"longitude":-122.92791,"time":"5:42:33 PM"},{"latitude":49.221062,"longitude":-122.923653,"time":"5:44:33 PM"},{"latitude":49.227119,"longitude":-122.912392,"time":"5:46:33 PM"},{"latitude":49.234489,"longitude":-122.89872,"time":"5:48:33 PM"},{"latitude":49.235699,"longitude":-122.894608,"time":"5:50:33 PM"},{"latitude":49.241928,"longitude":-122.89257,"time":"5:52:33 PM"},{"latitude":49.241871,"longitude":-122.89016,"time":"6:02:33 PM"}]}}

有人可以看看我做错了什么或建议修复吗?

【问题讨论】:

    标签: android arrays json


    【解决方案1】:

    试试这个:

    JSONObject obj = new JSONObject(jsonStr);
    
    JSONObject objData = obj.getJSONObject("data");
    JSONArray deviceJsonArray = objData.getJSONArray("coordinates");
    

    “坐标”JSONArray 是 JSON 结构中更深层次的一个元素

    【讨论】:

    • obj.getJSONObject("data") 分配给objData 会引发错误,因为一种类型为 JSONArray 而另一种类型为 JSONObject
    • 您的答案需要一个小修复。第二行应该是JSONObject objData = obj.getJSONObject("data");
    • @asus 谢谢!我复制/粘贴太快了,忽略了将对象类型更改为JSONObject
    猜你喜欢
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 2012-08-06
    • 1970-01-01
    • 1970-01-01
    • 2015-02-15
    相关资源
    最近更新 更多