解析为对象而不是数组时出现Json错误:JSONObject中的getJSONObject(java.lang.String)无法应用于(int)
Posted
技术标签:
【中文标题】解析为对象而不是数组时出现Json错误:JSONObject中的getJSONObject(java.lang.String)无法应用于(int)【英文标题】:Json Error when parsing as object rather than array :getJSONObject (java.lang.String) in JSONObject cannot be applied to (int) 【发布时间】:2019-04-23 10:25:43 【问题描述】:我之前的问题是“org.json.JSONObject 无法转换为 JSONArray”
所以我改变了这个 JSONArray stationJsonArray = newJSONArray(stationsJsonString);到这个 JSONObject stationJsonObject = new JSONObject(stationsJsonString);
根据this thread 中给出的答案,我的第一个问题已解决
这解决了最初的问题,但是现在我得到了这个问题 getJSONObject (java.lang.String) in JSONObject cannot be applied to (int)。我知道只有字符串值可以传递给 JSONObject 但这曾经是一个 JSONArray 所以我将如何使用一个数组而不导致在前一个线程中抛出 Json 异常。
老实说,我现在不知道如何解决这个问题。任何和所有的帮助将不胜感激。
这是我的代码:
private class AsyncTaskGetMareker extends AsyncTask<String , String, JSONObject>
@Override
protected void onPreExecute()
super.onPreExecute();
@Override
protected JSONObject doInBackground(String... strings)
String stationsJsonString = getJSONFromAssets();
try
JSONObject stationsJsonObject = new JSONObject(stationsJsonString);
return stationsJsonObject;
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.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 文件:
"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"
【问题讨论】:
【参考方案1】:注意:
JsonObject getJsonObject(String name) //parameter type should be String
for (int i =0; i <result.length(); i++)
JSONObject jsonObject= null;
try
jsonObject= result.getJSONObject(i);//but you pass an int
String name=jsonObject.getString("name");
【讨论】:
我知道,但是如果没有我链接的线程中显示的 JSONExeption,我该如何使用 JSONArray 来做到这一点以上是关于解析为对象而不是数组时出现Json错误:JSONObject中的getJSONObject(java.lang.String)无法应用于(int)的主要内容,如果未能解决你的问题,请参考以下文章
Newtonsoft:将 json 字符串解析为对象时出现“已超出读者的 MaxDepth 64”错误
使用 Alamofire 解析 json 时出现 AppDelegate 错误
复制 json 文件时出现 Amazon Redshift 错误 - JSONPath 格式无效:成员不是对象