我正在尝试解析java中的JSON字符串,以单独打印各个值。但是,在使程序运行时,出现以下错误-
Exception in thread "main" java.lang.RuntimeException: Stub! at org.json.JSONObject.<init>(JSONObject.java:7) at ShowActivity.main(ShowActivity.java:29)
我的班级看起来像-
import org.json.JSONException; import org.json.JSONObject; public class ShowActivity { private final static String jString = "{" + " \"geodata\": [" + " {" + " \"id\": \"1\"," + " \"name\": \"Julie Sherman\"," + " \"gender\" : \"female\"," + " \"latitude\" : \"37.33774833333334\"," + " \"longitude\" : \"-121.88670166666667\"" + " }" + " }," + " {" + " \"id\": \"2\"," + " \"name\": \"Johnny Depp\"," + " \"gender\" : \"male\"," + " \"latitude\" : \"37.336453\"," + " \"longitude\" : \"-121.884985\"" + " }" + " }" + " ]" + "}"; private static JSONObject jObject = null; public static void main(String[] args) throws JSONException { jObject = new JSONObject(jString); JSONObject geoObject = jObject.getJSONObject("geodata"); String geoId = geoObject.getString("id"); System.out.println(geoId); String name = geoObject.getString("name"); System.out.println(name); String gender=geoObject.getString("gender"); System.out.println(gender); String lat=geoObject.getString("latitude"); System.out.println(lat); String longit =geoObject.getString("longitude"); System.out.println(longit); } }
让我知道我丢失了什么,或者为什么每次运行该应用程序时都会得到该错误。任何意见,将不胜感激。
看我的评论。当以 android.jar身份运行时,您需要包含完整的org.json库。仅包含要针对其进行编译的存根。 __
此外,您还必须}在JSON数据中删除额外的两个实例longitude。
}
longitude
private final static String JSON_DATA = "{" + " \"geodata\": [" + " {" + " \"id\": \"1\"," + " \"name\": \"Julie Sherman\"," + " \"gender\" : \"female\"," + " \"latitude\" : \"37.33774833333334\"," + " \"longitude\" : \"-121.88670166666667\"" + " }," + " {" + " \"id\": \"2\"," + " \"name\": \"Johnny Depp\"," + " \"gender\" : \"male\"," + " \"latitude\" : \"37.336453\"," + " \"longitude\" : \"-121.884985\"" + " }" + " ]" + "}";
除此之外geodata,实际上不是a JSONObject而是a JSONArray。
geodata
JSONObject
JSONArray
这是经过全面测试并且经过测试的更正代码:
import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; public class ShowActivity { private final static String JSON_DATA = "{" + " \"geodata\": [" + " {" + " \"id\": \"1\"," + " \"name\": \"Julie Sherman\"," + " \"gender\" : \"female\"," + " \"latitude\" : \"37.33774833333334\"," + " \"longitude\" : \"-121.88670166666667\"" + " }," + " {" + " \"id\": \"2\"," + " \"name\": \"Johnny Depp\"," + " \"gender\" : \"male\"," + " \"latitude\" : \"37.336453\"," + " \"longitude\" : \"-121.884985\"" + " }" + " ]" + "}"; public static void main(final String[] argv) throws JSONException { final JSONObject obj = new JSONObject(JSON_DATA); final JSONArray geodata = obj.getJSONArray("geodata"); final int n = geodata.length(); for (int i = 0; i < n; ++i) { final JSONObject person = geodata.getJSONObject(i); System.out.println(person.getInt("id")); System.out.println(person.getString("name")); System.out.println(person.getString("gender")); System.out.println(person.getDouble("latitude")); System.out.println(person.getDouble("longitude")); } } }
这是输出:
C:\dev\scrap>java -cp json.jar;. ShowActivity 1 Julie Sherman female 37.33774833333334 -121.88670166666667 2 Johnny Depp male 37.336453 -121.884985