在Java中parsingJSONstring

我想parsing一个JSONstring在Java中有单独的值单独打印。 但是,当程序运行时,我得到以下错误 –

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库 ,只包含需要编译的存根。

另外,你必须在longitude删除JSON数据中的两个额外的}实例。

  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其实不是JSONObject而是JSONArray

这里是完整的工作和testing更正的代码:

 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 

看起来像你的两个对象(在数组内),在“经度”之后有一个额外的大括号。

首先在每个array object之后有一个额外的}

其次“地理数据”是一个JSONArray 。 所以,而不是JSONObject geoObject = jObject.getJSONObject("geodata"); 你必须得到它作为JSONArray geoObject = jObject.getJSONArray("geodata");

一旦你有了JSONArray你可以使用geoObject.get(<index>)获取JSONArray每个条目。

我正在使用org.codehaus.jettison.json

你在每个对象中都有一个额外的“ } ”,你可以这样写jsonstring:

 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\"" + " }" + " }" + " ]" + "}"; } 

这里是一个对象的例子,对于你的情况你必须使用JSONArray。

 public static final String JSON_STRING="{\"employee\":{\"name\":\"Sachin\",\"salary\":56000}}"; try{ JSONObject emp=(new JSONObject(JSON_STRING)).getJSONObject("employee"); String empname=emp.getString("name"); int empsalary=emp.getInt("salary"); String str="Employee Name:"+empname+"\n"+"Employee Salary:"+empsalary; textView1.setText(str); }catch (Exception e) {e.printStackTrace();} //Do when JSON has problem. } 

我没有时间,但试图给出一个想法。 如果你仍然做不到,那么我会帮忙。

纠正我,如果我错了,但JSON只是由“:”分隔的文本,所以只是使用

 String line = ""; //stores the text to parse. StringTokenizer st = new StringTokenizer(line, ":"); String input1 = st.nextToken(); 

继续使用st.nextToken()直到数据不足。 确保使用“st.hasNextToken()”,所以你不会得到一个空的exception。