提取JSON TypeMismatch

时间:2014-11-24 10:48:44

标签: java arrays json map polyline

我在尝试提取从JSON返回的数据并存储为Polyline对象时遇到了一些问题。 JSON格式如下:

 {
"messages": [

 ],
 "routes": {
  "fieldAliases": {
   "ObjectID": "ObjectID",
   "Name": "Name",
   "FirstStopID": "FirstStopID",
   "LastStopID": "LastStopID",
   "StopCount": "StopCount",
   "Total_Meters": "Total_Meters",
   "Shape_Length": "Shape_Length"
  },
  "geometryType": "esriGeometryPolyline",
  "spatialReference": {
   "wkid": 3414,
   "latestWkid": 3414
  },
  "features": [
   {
    "attributes": {
     "ObjectID": 1,
     "Name": "18304.68,36152.73 - 21591.48,33095.24",
     "FirstStopID": 1,
     "LastStopID": 2,
     "StopCount": 2,
     "Total_Meters": 6757.9427813693819,
     "Shape_Length": 6757.9404808662866
    },
    "geometry": {
     "paths": [
      [
       [
        18301.44000000041,
        36146.919999999925
       ],
   [
    21206.919999999925,
    33505.550000000745
   ],
   [
    21341.230000000447,
    33566.929999999702
   ],

我得到返回的JSON并将每个点存储为Polyline对象的代码:

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 = "{\'EventDirection\':" + responseString + "}";
        try {
            JSONObject jsonObject = new JSONObject(page);
            jsonArray = jsonObject.getJSONArray("EventDirection");
            int length = jsonArray.length();
            for (int i = 0; i < length; i++) {
                JSONObject attribute = jsonArray.getJSONObject(i);
                if(attribute.equals("features")){
                if(attribute.equals("geometry")){
                    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);
            ENeighbourhoodActivity.graphicsLayer.addGraphic(lineGraphic);   
        } catch (JSONException e) {
            e.printStackTrace();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }   
    }

我已经得到了堆栈跟踪:

11-24 20:32:19.425: D/dalvikvm(16607): GC_CONCURRENT freed 719K, 13% free 12030K/13703K, paused 7ms+13ms
11-24 20:32:19.628: W/System.err(16607): org.json.JSONException: Value {"directions":[{"features":[{"compressedGeometry":"+1+hrt+139j+0+0","attributes":{"length":0,"text":"Start at 18304.68,36152.73","time":0,"maneuverType":"esriDMTDepart","ETA":-2209161600000}},{"compressedGeometry":"+1+hrt+139j+172-lr+40-1i+1l-e+23-3+d+4+1d+r","attributes":{"length":1.7571306436022183,"text":"Go southeast on PAN ISLAND EXPRESSWAY","time":1.8785585885556129,"maneuverType":"esriDMTStraight","ETA":-2209161600000}},{"compressedGeometry":"+1+jch+12ik+i+1o+d+1v","attributes":{"length":0.12315161844398603,"text":"Bear left on TOH TUCK AVENUE","time":0.18480000000000038,"maneuverType":"esriDMTBearLeft","ETA":-2209161600000}},{"compressedGeometry":"+1+jdg+12mb+14-1+42+1b+p+m+11+2a+c+e+l+e+r+9+1q+2+o+5+1m+e+1v+o+3u+c+1u+0+1q-6+3a-i+11+2+1e+k+1k+12+1o+o+p+a+1p+a+m+8+m+b+2d+s+1g+t","attributes":{"length":1.3971933538812136,"text":"Turn right on TOH TUCK ROAD","time":2.7943955099999993,"maneuverType":"esriDMTTurnRight","ETA":-2209161600000}},{"compressedGeometry":"+1+kl9+132b+1l-1i+19-o+g-3+2e-8+3h-5+44-2+1i+4+1r+u+v+11+2v+43","attributes":{"length":0.78371010402386,"text":"Turn right on JALAN JURONG KECHIL","time":1.175752590000001,"maneuverType":"esriDMTTurnRight","ETA":-2209161600000}},{"compressedGeometry":"+1+la5+135l+1g-23+29-2o+1f-2d+d-1b+0-23-b-17-k-1n+0-h+8-l+o-r","attributes":{"length":0.5739351553531507,"text":"Turn right on JALAN ANAK BUKIT","time":0.9289808300000004,"maneuverType":"esriDMTTurnRight","ETA":-2209161600000}},{"compressedGeometry":"+1+lfr+12m0+dd-4m+qr-94+19n-ej+1j-n+82-56+gl-b4+5l-1a+3n-b+q-5+i-4+2k-13+4t-l+9p-14+co-3a+1r-j+1k-4+5o-1t+u-3+c+6+i+f+d+h","attributes":{"length":5.512234370362195,"text":"Bear left on DUNEARN ROAD","time":8.347599999999996,"maneuverType":"esriDMTBearLeft","ETA":-2209161600000}},{"compressedGeometry":"+1+qge+10ut+10+30+2p+5h","attributes":{"length":0.2995301962581563,"text":"Bear left on WHITLEY ROAD","time":0.4492,"maneuverType":"esriDMTBearLeft","ETA":-2209161600000}},{"compressedGeometry":"+1+qk7+117e+h-4+1p-1a+29-2o+j-b+q-2+b+4+h+c+c+h+10+2a+b+d+e+8+3n+3","attributes":{"length":0.5376352927205378,"text":"Turn right on MALCOLM ROAD","time":1.2903999999999978,"maneuverType":"esriDMTTurnRight","ETA":-2209161600000}},{"compressedGeometry":"+1+r0v+116u+f-k+l-a+t-1+s+7+1i+j+u+h+m+g+o+p+11+1f+11+u","attributes":{"length":0.35816860827144775,"text":"Turn right at UNNAMED ROAD to stay on MALCOLM ROAD","time":0.8596014644118043,"maneuverType":"esriDMTTurnRight","ETA":-2209161600000}},{"compressedGeometry":"+1+r9s+11b0+0+0","attributes":{"length":0,"text":"Finish at 27985.4,34115, on the right","time":0,"maneuverType":"esriDMTStop","ETA":-2209161600000}}],"summary":{"totalDriveTime":17.909288982967414,"totalTime":17.909288986120373,"envelope":{"ymin":33719.41002910024,"ymax":36152.73002910059,"xmin":18301.436076220416,"spatialReference":{"latestWkid":3414,"wkid":3414},"xmax":27985.400018159162},"totalLength":11.342689342916765},"routeName":"18304.68,36152.73 - 27985.4,34115","routeId":1}],"routes":{"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.439
11-24 20:32:19.651: W/System.err(16607):    at org.json.JSON.typeMismatch(JSON.java:100)
11-24 20:32:19.651: W/System.err(16607):    at org.json.JSONObject.getJSONArray(JSONObject.java:548)
11-24 20:32:19.651: W/System.err(16607):    at Controller.EventController.getDirection(EventController.java:222)
11-24 20:32:19.651: W/System.err(16607):    at AsyncTask.GetEventDirectionAsyncTask.doInBackground(GetEventDirectionAsyncTask.java:21)
11-24 20:32:19.651: W/System.err(16607):    at AsyncTask.GetEventDirectionAsyncTask.doInBackground(GetEventDirectionAsyncTask.java:1)
11-24 20:32:19.651: W/System.err(16607):    at android.os.AsyncTask$2.call(AsyncTask.java:264)
11-24 20:32:19.651: W/System.err(16607):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
11-24 20:32:19.651: W/System.err(16607):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
11-24 20:32:19.651: W/System.err(16607):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
11-24 20:32:19.651: W/System.err(16607):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
11-24 20:32:19.651: W/System.err(16607):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
11-24 20:32:19.651: W/System.err(16607):    at java.lang.Thread.run(Thread.java:856)
11-24 20:32:40.862: W/SurfaceView(16607): CHECK surface infomation creating=false formatChanged=false sizeChanged=false visible=false visibleChanged=true surfaceChanged=true realSizeChanged=false redrawNeeded=false left=false top=false

任何指南?提前谢谢。

修改

try {
            JSONObject jsonObject = new JSONObject(page);
            JSONObject myObject = jsonObject.getJSONObject("routes");
            JSONArray features = myObject.getJSONArray("features");
            JSONObject geometry = features.getJSONObject(1);
            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 :(得分:0)

问题在于您获得的JSON是以下内容:(从堆栈跟踪中获取的摘录):您可以看到数据结构与您的预期完全不同:

浏览的方法是了解哪个部分是JSONObject,哪个部分是JSONArray ......这很容易:看看我的评论

{ //<--------- global JSONObject
"directions" : [
    {
        "features" : [ //<---------it starts with [, so it's an array
            {
                "compressedGeometry" : "+1+hrt+139j+0+0",
                "attributes" : {
                    "length" : 0,
                    "text" : "Start at 18304.68,36152.73",
                    "time" : 0,
                    "maneuverType" : "esriDMTDepart",
                    "ETA" : -2209161600000
                }
            }, 
            ... //a lot of items here
        ], //end of features
        "summary" : { //<---------curly braces mean JSONObject !
            "totalDriveTime" : 17.909288982967414,
            "totalTime" : 17.909288986120373,
            "envelope" : {
                "ymin" : 33719.41002910024,
                "ymax" : 36152.73002910059,
                "xmin" : 18301.436076220416,
                "spatialReference" : {
                    "latestWkid" : 3414,
                    "wkid" : 3414
                },
                "xmax" : 27985.400018159162
            },
            "totalLength" : 11.342689342916765
        },
        "routeName" : "18304.68,36152.73 - 27985.4,34115",
            "routeId" : 1
    }
], //<------- end of the array directions
"routes" : { //<---------curly braces mean routes is an JSONObject !
    "features" : [{ // features is an array, which contains objects where its first attribute is a JSONObject named geometry
                "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.439

所以你有一个JSON对象,它具有以下属性:directionsroutes,以及routes是另一个具有名为features的属性的对象,它是一个数组每个元素的第一个属性必须为geometry的元素,这是第一个属性为path的对象...除以你将获胜