对不起重复的问题。我几个小时以来一直在敲打这个问题。 我试图将json解析为setter / getter类的对象。
String location = request.getParameter("location");
System.out.print(location);
org.json.JSONObject json = new org.json.JSONObject(location);
/* JSONObject js = json.getJSONObject("locationInfo");
String mapLoc = js.getString("locationInfo"); */
String mapLoc = json.getString("locationInfo"); <--- receiving the error on this line
Gson gsn = new Gson();
MapObject mLocObj = gsn.fromJson(mapLoc, MapObject.class);
System.out.println(mLocObj.getLongitude());
double longitude = Double.parseDouble(mLocObj.getLongitude());
double latitude = Double.parseDouble(mLocObj.getLatitude());
这是我的json字符串位置:
{"locationInfo":{"latitude":"19.3828815","longitude":"72.8296205"}}
这是错误日志:
19: System.out.print(location);
20:
21: org.json.JSONObject json = new org.json.JSONObject(location);
22: String mapLoc = json.getString("locationInfo");
23:
24: Gson gsn = new Gson();
25: MapObject mLocObj = gsn.fromJson(mapLoc, MapObject.class);
Stacktrace:] with root cause
org.json.JSONException: JSONObject["locationInfo"] not a string.
at org.json.JSONObject.getString(JSONObject.java:639)
at org.apache.jsp.mapView_jsp._jspService(mapView_jsp.java:88)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:307)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
at java.lang.Thread.run(Thread.java:619)
答案 0 :(得分:4)
您需要检查您的&#34; locationInfo&#34; string为null:
if (json.isNull("locationInfo")){
mapLoc = "";
} else {
mapLoc = json.getString("locationInfo");
}
答案 1 :(得分:3)
在json位置是另一个json对象。
所以你会使用:
json.getJSONObject("locationInfo")
此外,对于纬度:
json.getJSONObject("locationInfo").getString("latitude")
答案 2 :(得分:1)
我认为问题在于,“locationInfo”实际上不是字符串而是JSONObject。
你可以尝试
org.json.JSONObject jsonlocationInfo = json.getJSONObject("locationInfo");
答案 3 :(得分:0)
我认为你有两个不同的库json.org和goolge gson。通过使用gson,您可以获得locationIfno字符串,如下所示
JsonParser jsonParser = new JsonParser();
JsonElement jsonElement = jsonParser.parse(location);
String locationInfo = jsonElement.getAsJsonObject().get("locationInfo").toString();
{"latitude":"19.3828815","longitude":"72.8296205"}
最后解析它
Gson gsn = new Gson();
MapObject mLocObj = gsn.fromJson(locationInfo , MapObject.class);