首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >解析本地Json文件时的JsonExeption

解析本地Json文件时的JsonExeption
EN

Stack Overflow用户
提问于 2018-11-20 16:40:16
回答 1查看 35关注 0票数 0

我试图使用本地Json文件在google上显示标记。问题是我得到了这个错误。

W/System.err: org.json.JSONException: Value {“预测”:[{“描述”:“游戏Paisley,High,Paisley,UK","id":"d4fafbac99296d0a545170bb9af984e6251ac917",”matched_substrings“:{”长度“:4,”偏移“:0},"place_id":"ChIJN0z6gTdJiEgREKQBicnWcAs","reference":"ChIJN0z6gTdJiEgREKQBicnWcAs",”structured_formatting“:{”main_text“:”游戏Paisley","main_text_matched_substrings":{"length":4,“偏移”:0},“secondary_text”:“高街,佩斯利,英国”},“术语”:{“偏移”:0,“价值”:“游戏Paisley"},{”偏移“:14,”值“:”高街“},{”偏移“:27,”值“:”Paisley“},{”偏移“:36,”值“:”UK"},“类型”:“建立”}},{“描述”:“游戏格拉斯哥联合街”,联合街,英国格拉斯哥,"id":"9579c1d2df7575a2eb479ccd987cf29ab0ab9c6e",“matched_substrings”:{“长度”:4,“偏移”:0},"place_id":"ChIJ2YlAjJ5GiEgRu7aBLOFlkkc","reference":"ChIJ2YlAjJ5GiEgRu7aBLOFlkkc",“structured_formatting”:{“main_text”:“游戏格拉斯哥联合街”,“main_text_matched_substrings”:{“长度”:4,“偏移”:0},“secondary_text”:“联合街,格拉斯哥,英国”}“术语”:{“偏移”:0,“值”:“游戏格拉斯哥联合街”},{“偏移”:27,“值”:“联合街”},{“偏移”:41,“值”:“格拉斯哥”},{“偏移”:50,“值”:“UK”},“类型”:“建立”},{“描述”:“游戏,圣以诺广场,格拉斯哥,英国”,"id":"582edc64f3d6654d8ff9a8da364c37a8e0544102",“matched_substrings”:{“长度”:4,“偏移”:0},"place_id":"ChIJq2yljp5GiEgRpsnAfUFKGEA",“引用”:“ChIJq2yljp5GiEgRpsnAfUFKGEA”,“structured_formatting”:{“main_text”:“游戏”,“main_text_matched_substrings”:{“长度”:4,“偏移”:0},“secondary_text”:“secondary_text”:“圣伊诺克广场,英国”},“术语”:{“偏移”:0,“值”:“游戏”},{“偏移”:6,“价值”:“圣伊诺克广场”},{“偏移”:26,“值”:“格拉斯哥”},{“偏移”:35,“值”:“英国”},“类型”:“建立”},{“描述”:“描述”:“游戏格拉斯哥国王的英寸路,格拉斯哥国王之路,格拉斯哥,英国,"id":"b60dbbc529f25226dfee4c05c80de8e92137588d",”matched_substrings:{“长度”:4,“偏移”:0},"place_id":"ChIJv2Te_ENPiEgRknxQydZhR8Q","reference":"ChIJv2Te_ENPiEgRknxQydZhR8Q",“structured_formatting”:{“main_text”:“游戏格拉斯哥国王的英制路”,“main_text_matched_substrings”:{“长度”:4,“偏移”:0},“secondary_text”:“国王的英制路,格拉斯哥,英国”},“术语”:{“偏移”:0,“值”:“游戏格拉斯哥国王的英制路”}{“偏移”:31,“价值”:“国王的英寸路”},{“偏移”:49,“值”:“格拉斯哥”},{“偏移”:58,“值”:“UK”},“类型”:“建立”},{“描述”:“游戏,柯格林路,格拉斯哥,英国”,"id":"86a4ed10b3b9b609f68533f1c47dc8f9ae5d2aee",“matched_substrings”:{“长度”:4,偏移:0},"place_id":"ChIJT3vEiYRIiEgR4EIqP8jmL6o",“参考”:“ChIJT3vEiYRIiEgR4EIqP8jmL6o”、“structured_formatting”:{“main_text”:“main_text”、“main_text_matched_substrings”:{“长度”:4、“偏移”:0}、"secondary_text":"Cowglen Road,Glasgow“}、”terms“:{”偏移“:0、”value“、{”偏移“:6、”值“:”Cowglen Road"}、{“偏移”:20、“值”:“Glasgow”},{“偏移”:29,“值”:“UK”},“类型”:“单位”}],“org.json.JSONObject”类型的“OK”}不能转换为JSONArray W/System.err: at org.json.JSON.typeMismatch(JSON.java:111) at org.json.JSONArray.(JSONArray.java:96) at org.json.JSONArray.(JSONArray.java:108) at nearbyplaces.pnai at nearbyplaces.pnai.uws.com.maplocation.MapsActivity$AsyncTaskGetMareker.doInBackground(MapsActivity.java:102) android.os.AsyncTask$2.call(AsyncTask.java:304) W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243) W/System.err:在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) at java.lang.Thread.run(Thread.java:762)

这是我的地图活动代码

代码语言:javascript
复制
  @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_maps);
            // Obtain the SupportMapFragment and get notified when the map is ready to be used.
            SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map);
            mapFragment.getMapAsync(this);

        }
        @Override
        public void onMapReady(GoogleMap googleMap) {
            mMap = googleMap;

            gps = new TrackGPS(MapsActivity.this);
            new AsyncTaskGetMareker().execute();
            /*
            if(gps.canGetLocation()){
                currentLoc= new LatLng(gps.getLatitude(),gps.getLongitude());
                Log.d(TAG, "onMapReady: Got Location: " +gps.getLatitude() + " " + gps.getLongitude());
                // Add a marker in Sydney and move the camera
                mMap.addMarker(new MarkerOptions()
                        .position(currentLoc)
                        .title("CurrentLocation"));
                Log.d(TAG, "onMapReady: Displaying Map");
                mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(currentLoc,15));
            }else{
                gps.showAlert();
            }
            */
        }

        public String getJSONFromAssets() {
            String json = null;
            try {
                InputStream inputData = getAssets().open("gameStores.json");
                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();
                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("name");
                            String lat=jsonObject.getString("lat");
                            String lang=jsonObject.getString("lang");

                            drawMarker(new LatLng(Double.parseDouble(lat),
                                    Double.parseDouble(lang)), name);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }

            }

            private void drawMarker(LatLng point, String name) {
                MarkerOptions markerOptions = new MarkerOptions();
                markerOptions.position(point);
                markerOptions.snippet(name);
                mMap.addMarker(markerOptions);
            }
        }

这是我的json文件

代码语言:javascript
复制
 {    "predictions" : [
>       {
>          "description" : "GAME Paisley, High Street, Paisley, UK",
>          "id" : "d4fafbac99296d0a545170bb9af984e6251ac917",
>          "matched_substrings" : [
>             {
>                "length" : 4,
>                "offset" : 0
>             }
>          ],
>          "place_id" : "ChIJN0z6gTdJiEgREKQBicnWcAs",
>          "reference" : "ChIJN0z6gTdJiEgREKQBicnWcAs",
>          "structured_formatting" : {
>             "main_text" : "GAME Paisley",
>             "main_text_matched_substrings" : [
>                {
>                   "length" : 4,
>                   "offset" : 0
>                }
>             ],
>             "secondary_text" : "High Street, Paisley, UK"
>          },
>          "terms" : [
>             {
>                "offset" : 0,
>                "value" : "GAME Paisley"
>             },
>             {
>                "offset" : 14,
>                "value" : "High Street"
>             },
>             {
>                "offset" : 27,
>                "value" : "Paisley"
>             },
>             {
>                "offset" : 36,
>                "value" : "UK"
>             }
>          ],
>          "types" : [ "establishment" ]
>       },
>       {
>          "description" : "GAME Glasgow Union Street, Union Street, Glasgow, UK",
>          "id" : "9579c1d2df7575a2eb479ccd987cf29ab0ab9c6e",
>          "matched_substrings" : [
>             {
>                "length" : 4,
>                "offset" : 0
>             }
>          ],
>          "place_id" : "ChIJ2YlAjJ5GiEgRu7aBLOFlkkc",
>          "reference" : "ChIJ2YlAjJ5GiEgRu7aBLOFlkkc",
>          "structured_formatting" : {
>             "main_text" : "GAME Glasgow Union Street",
>             "main_text_matched_substrings" : [
>                {
>                   "length" : 4,
>                   "offset" : 0
>                }
>             ],
>             "secondary_text" : "Union Street, Glasgow, UK"
>          },
>          "terms" : [
>             {
>                "offset" : 0,
>                "value" : "GAME Glasgow Union Street"
>             },
>             {
>                "offset" : 27,
>                "value" : "Union Street"
>             },
>             {
>                "offset" : 41,
>                "value" : "Glasgow"
>             },
>             {
>                "offset" : 50,
>                "value" : "UK"
>             }
>          ],
>          "types" : [ "establishment" ]
>       },
>       {
>          "description" : "Game, Saint Enoch Square, Glasgow, UK",
>          "id" : "582edc64f3d6654d8ff9a8da364c37a8e0544102",
>          "matched_substrings" : [
>             {
>                "length" : 4,
>                "offset" : 0
>             }
>          ],
>          "place_id" : "ChIJq2yljp5GiEgRpsnAfUFKGEA",
>          "reference" : "ChIJq2yljp5GiEgRpsnAfUFKGEA",
>          "structured_formatting" : {
>             "main_text" : "Game",
>             "main_text_matched_substrings" : [
>                {
>                   "length" : 4,
>                   "offset" : 0
>                }
>             ],
>             "secondary_text" : "Saint Enoch Square, Glasgow, UK"
>          },
>          "terms" : [
>             {
>                "offset" : 0,
>                "value" : "Game"
>             },
>             {
>                "offset" : 6,
>                "value" : "Saint Enoch Square"
>             },
>             {
>                "offset" : 26,
>                "value" : "Glasgow"
>             },
>             {
>                "offset" : 35,
>                "value" : "UK"
>             }
>          ],
>          "types" : [ "establishment" ]
>       },
>       {
>          "description" : "GAME Glasgow King's Inch Road, King's Inch Road, Glasgow, UK",
>          "id" : "b60dbbc529f25226dfee4c05c80de8e92137588d",
>          "matched_substrings" : [
>             {
>                "length" : 4,
>                "offset" : 0
>             }
>          ],
>          "place_id" : "ChIJv2Te_ENPiEgRknxQydZhR8Q",
>          "reference" : "ChIJv2Te_ENPiEgRknxQydZhR8Q",
>          "structured_formatting" : {
>             "main_text" : "GAME Glasgow King's Inch Road",
>             "main_text_matched_substrings" : [
>                {
>                   "length" : 4,
>                   "offset" : 0
>                }
>             ],
>             "secondary_text" : "King's Inch Road, Glasgow, UK"
>          },
>          "terms" : [
>             {
>                "offset" : 0,
>                "value" : "GAME Glasgow King's Inch Road"
>             },
>             {
>                "offset" : 31,
>                "value" : "King's Inch Road"
>             },
>             {
>                "offset" : 49,
>                "value" : "Glasgow"
>             },
>             {
>                "offset" : 58,
>                "value" : "UK"
>             }
>          ],
>          "types" : [ "establishment" ]
>       },
>       {
>          "description" : "GAME, Cowglen Road, Glasgow, UK",
>          "id" : "86a4ed10b3b9b609f68533f1c47dc8f9ae5d2aee",
>          "matched_substrings" : [
>             {
>                "length" : 4,
>                "offset" : 0
>             }
>          ],
>          "place_id" : "ChIJT3vEiYRIiEgR4EIqP8jmL6o",
>          "reference" : "ChIJT3vEiYRIiEgR4EIqP8jmL6o",
>          "structured_formatting" : {
>             "main_text" : "GAME",
>             "main_text_matched_substrings" : [
>                {
>                   "length" : 4,
>                   "offset" : 0
>                }
>             ],
>             "secondary_text" : "Cowglen Road, Glasgow, UK"
>          },
>          "terms" : [
>             {
>                "offset" : 0,
>                "value" : "GAME"
>             },
>             {
>                "offset" : 6,
>                "value" : "Cowglen Road"
>             },
>             {
>                "offset" : 20,
>                "value" : "Glasgow"
>             },
>             {
>                "offset" : 29,
>                "value" : "UK"
>             }
>          ],
>          "types" : [ "establishment" ]
>       }    ],    "status" : "OK" }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-11-20 16:54:28

org.json.JSONObject不能转换为JSONArray

根节点是一个JSONObject,但是您正在使用JSONArray解析它。

替换

代码语言:javascript
复制
JSONArray stationsJsonArray = new JSONArray(stationsJsonString);

使用

代码语言:javascript
复制
JSONObject stationsJsonObject = new JSONObject(stationsJsonString);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53397588

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档