提取JSON TypeMismatch错误消息

时间:2014-11-24 14:44:28

标签: java android json

尝试从JSON中提取数据时遇到了一些问题。基本上,这是我的JSON格式:

http://www.onemap.sg/API/services.svc/route/solve?token=qo/s2TnSUmfLz+32CvLC4RMVkzEFYjxqyti1KhByvEacEdMWBpCuSSQ+IFRT84QjGPBCuz/cBom8PfSm3GjEsGc8PkdEEOEr&routeStops=18304.68,36152.73;27985.4,34115&routemode=DRIVE&avoidERP=0&routeOption=shortest

以下是代码:

public void getDirection(Event eventModel){
    String eventX = eventModel.getEventX();
    Log.i("X", eventX);

    String eventY = eventModel.getEventY();
    Log.i("Y", eventY);

    SimpleLineSymbol lineSymbol = new SimpleLineSymbol(
            Color.GREEN, 3, SimpleLineSymbol.STYLE.DASH);
    List pointArr = null;
    String page;
    JSONArray jsonArray;

    try {
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet("http://www.onemap.sg/API/services.svc/route/solve?token=qo/s2TnSUmfLz+32CvLC4RMVkzEFYjxqyti1KhByvEacEdMWBpCuSSQ+IFRT84QjGPBCuz/cBom8PfSm3GjEsGc8PkdEEOEr&routeStops=18304.68,36152.73;" + eventX + "," + eventY + "&routemode=DRIVE&avoidERP=0&routeOption=shortes");
        HttpResponse response = client.execute(request);
        HttpEntity entity = response.getEntity();
        String responseString = EntityUtils.toString(entity, "UTF-8");
        page = responseString ;
        try {
            JSONObject jsonObject = new JSONObject(page);
            jsonArray = jsonObject.getJSONArray("routes");
            Log.i("Array", jsonArray.toString());
            JSONObject features = jsonArray.getJSONObject(3);
            //String geometry = features.getString("geometry");
            JSONArray paths = features.getJSONArray("paths");
            int length = paths.length();
            for (int i = 0; i < length; i++) {
                JSONObject attribute = paths.getJSONObject(i);
                    String path = attribute.getString("paths");
                    pointArr.add(path);
                    /*path = path.replace("[[\"", "");
                    path = path.replace("\"]]", "");
                    String[] arr = path.split(";");
                    for (int j = 0; j < arr.length; j++) {
                        String[] point = arr[j].split(",");
                        pointArr.add(point);
                    }*/

            }
            Graphic lineGraphic = new Graphic((Geometry) pointArr, lineSymbol);
            graphicsLayer.addGraphic(lineGraphic);  
        } catch (JSONException e) {
            e.printStackTrace();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

但是,我收到typeMismatch错误消息为LogCat:

11-24 22:42:23.635: D/dalvikvm(4206): GC_CONCURRENT freed 627K, 12% free 11998K/13575K, paused 2ms+3ms
11-24 22:42:23.675: W/System.err(4206): org.json.JSONException: Value {"features":[{"geometry":{"paths":[[[18301.44000000041,36146.919999999925],[18885.830000000075,35820.75999999978],[19550.639999999665,35448.38000000082],[19679.16000000015,35397.5700000003],[19731.700000000186,35383.86999999918],[19799.240000000224,35381.169999999925],[19812.37999999989,35385.49000000022],[19828.570000000298,35394.31000000052],[19856.860000000335,35412.300000000745],[19875.230000000447,35468.13000000082],[19885.419999999925,35519.13000000082],[19887.83999999985,35531.25999999978],[19924.03000000026,35530.199999999255],[20053.900000000373,35572.88000000082],[20071.599999999627,35586.460000000894],[20078.78000000026,35595.31000000052],[20088.94000000041,35614.65000000037],[20111.830000000075,35668.51999999955],[20124.389999999665,35682.72000000067],[20145.12999999989,35697.40000000037],[20171.889999999665,35706.49000000022],[20229.700000000186,35708.38000000082],[20254.44000000041,35712.52999999933],[20308.200000000186,35726.9299999997],[20361.94000000041,35748.6799999997],[20371.480000000447,35751.43999999948],[20381.28000000026,35752.33999999985],[20496.959999999963,35762.949999999255],[20558.75,35763.08999999985],[20617.179999999702,35757.47000000067],[20723.37999999989,35739.27999999933],[20755.53000000026,35741.210000000894],[20802.16000000015,35760.75],[20853.950000000186,35794.74000000022],[20909.53000000026,35818.99000000022],[20935.05999999959,35828.8200000003],[20991.87999999989,35839.109999999404],[21014.44000000041,35846.91000000015],[21035.929999999702,35858],[21113.44000000041,35885.74000000022],[21161.209999999963,35914.98000000045],[21167.639999999665,35908.5],[21193.66000000015,35882.289999999106],[21206.96999999974,35871.199999999255],[21214.040000000037,35865.31000000052],[21221.080000000075,35861.22000000067],[21255.269999999553,35841.330000000075],[21270.78000000026,35838.24000000022],[21348.799999999814,35829.50999999978],[21376.360000000335,35828.52999999933],[21391.58999999985,35827.98000000045],[21419.820000000298,35826.98000000045],[21429.179999999702,35826.640000000596],[21437.96999999974,35826.330000000075],[21462.459999999963,35825.449999999255],[21594.21999999974,35822.75999999978],[21615.230000000447,35823.02999999933],[21644.009999999776,35826.50999999978],[21665.08999999985,35835.580000000075],[21703.19000000041,35857.4299999997],[21733.599999999627,35890.11999999918],[21775.389999999665,35945.84999999963],[21793.290000000037,35971.830000000075],[21810.580000000075,35996.539999999106],[21829.040000000037,36021.0700000003],[21848.650000000373,35997.11999999918],[21877.200000000186,35954.09999999963],[21950.490000000224,35865.90000000037],[21967.570000000298,35841.419999999925],[21997.03000000026,35789.26999999955],[22010.419999999925,35746.449999999255],[22011.799999999814,35714.11999999918],[22009.5,35679.16000000015],[21998.87999999989,35639.90000000037],[21986.639999999665,35606.86999999918],[21978.55999999959,35585.0700000003],[21979.12999999989,35568.11999999918],[21987.16000000015,35547.419999999925],[21998.05999999959,35533.59999999963],[22010.80999999959,35519.83999999985],[22178.459999999963,35460.65000000037],[22203.580000000075,35454.00999999978],[22368.80999999959,35396.36999999918],[22439.62999999989,35370.25],[22462.429999999702,35363.06000000052],[22536.71999999974,35337.8200000003],[22663.389999999665,35296.06000000052],[22783.040000000037,35254.419999999925],[22872.87000000011,35223.460000000894],[22905.179999999702,35212.34999999963],[22966.46999999974,35191.27999999933],[23098.830000000075,35146.419999999925],[23177.959999999963,35119.49000000022],[23298.53000000026,35078.460000000894],[23655.519999999553,34952.699999999255],[23677.139999999665,34945.109999999404],[23841.41000000015,34887.9299999997],[23862.490000000224,34880.550000000745],[23887.480000000447,34871.83999999985],[24026.480000000447,34822.210000000894],[24033.759999999776,34819.710000000894],[24057.400000000373,34811.580000000075],[24099.049999999814,34797.210000000894],[24120.46999999974,34789.9299999997],[24235.62999999989,3475
11-24 22:42:24.128: W/System.err(4206):     at org.json.JSON.typeMismatch(JSON.java:100)
11-24 22:42:24.128: W/System.err(4206):     at org.json.JSONObject.getJSONArray(JSONObject.java:548)
11-24 22:42:24.128: W/System.err(4206):     at Controller.EventController.getDirection(EventController.java:222)
11-24 22:42:24.128: W/System.err(4206):     at AsyncTask.GetEventDirectionAsyncTask.doInBackground(GetEventDirectionAsyncTask.java:21)
11-24 22:42:24.128: W/System.err(4206):     at AsyncTask.GetEventDirectionAsyncTask.doInBackground(GetEventDirectionAsyncTask.java:1)
11-24 22:42:24.135: W/System.err(4206):     at android.os.AsyncTask$2.call(AsyncTask.java:264)
11-24 22:42:24.135: W/System.err(4206):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
11-24 22:42:24.135: W/System.err(4206):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
11-24 22:42:24.135: W/System.err(4206):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
11-24 22:42:24.135: W/System.err(4206):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
11-24 22:42:24.135: W/System.err(4206):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
11-24 22:42:24.135: W/System.err(4206):     at java.lang.Thread.run(Thread.java:856)

任何指南?提前谢谢。

修改

JSONObject jsonObject = new JSONObject(page);
            /*JSONObject myObject = jsonObject.getJSONObject("routes");
            JSONArray features = jsonArray.getJSONArray(3);*/
            JSONObject geometry = jsonObject.getJSONObject("geometry");
            jsonArray = geometry.getJSONArray("paths");
            int length = geometry.length();
            for (int i = 0; i < length; i++) {

                JSONObject attribute = jsonArray.getJSONObject(i);
                    String path = attribute.getString("paths");
                    path = path.replace("[[\"", "");
                    path = path.replace("\"]]", "");
                    String[] arr = path.split(";");
                    for (int j = 0; j < arr.length; j++) {
                        String[] point = arr[j].split(",");
                        pointArr.add(point);
                    }

            }

1 个答案:

答案 0 :(得分:1)

&#34;路线&#34;是一个对象,而不是一个数组。

JSONObject myObject = jsonObject.getJSONObject("routes");