Java JSONObject["locationInfo"] 不是字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23291015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSONObject["locationInfo"] not a string
提问by sanedroid
Sorry for the repeated question.I have been banging my head on this since hours. I am trying to parse json to an object of setter/getter class.
很抱歉重复提问。几个小时以来,我一直在思考这个问题。我正在尝试将 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());
Here is my json string location:
这是我的 json 字符串位置:
{"locationInfo":{"latitude":"19.3828815","longitude":"72.8296205"}}
This is the error log:
这是错误日志:
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)
采纳答案by sanados
In json location is another json object.
在 json 位置是另一个 json 对象。
so you would use:
所以你会使用:
json.getJSONObject("locationInfo")
furthermore for latitude then:
此外对于纬度:
json.getJSONObject("locationInfo").getString("latitude")
回答by zeppaman
I think the problem is that in fact "locationInfo" is not a string but a JSONObject.
我认为问题在于实际上“locationInfo”不是字符串而是JSONObject。
Take a look at documentation
看一下文档
You could try to get
你可以尝试得到
org.json.JSONObject jsonlocationInfo = json.getJSONObject("locationInfo");
回答by Asif Bhutto
I think you have two different libraries json.org and goolge gson. By using gson you can get locationIfno string as below
我认为您有两个不同的库 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"}
finally parse it
最后解析它
Gson gsn = new Gson();
MapObject mLocObj = gsn.fromJson(locationInfo , MapObject.class);
回答by IgorGanapolsky
You need to check whether your "locationInfo" string is null:
您需要检查您的“locationInfo”字符串是否为空:
if (json.isNull("locationInfo")){
mapLoc = "";
} else {
mapLoc = json.getString("locationInfo");
}