我的 XML 文件看起来像这样
<Reading>
<Item Month="_October.xml">
<kWhReading Firstday="1" />
<kWhReading Lastday="552" />
</Item>
</Reading>
然后使用这行代码
JSONArray jsonArray = (JSONArray) JSONSerializer.toJSON( json.toString() );
我懂了
[{"@Month":"_October.xml","kWhReading":[{"@Firstday":"1"},{"@Lastday":"552"}]}]
然后,使用这个
JSONObject jsonobject = (JSONObject) jsonArray.get(0);
我懂了
{"@Month":"_October.xml","kWhReading":[{"@Firstday":"1"},{"@Lastday":"552"}]}
现在的问题是,我想Lastday
从kWhReading
using中获得价值
JSONObject readingkWhLast = jsonobject.getJSONObject("kWhReading");
但它不起作用,它抛出异常
net.sf.json.JSONException: JSONObject["kWhReading"] is not a JSONObject.
at net.sf.json.JSONObject.getJSONObject(JSONObject.java:2058)
at com.syntronic.bean.RecentUsage.doPost(RecentUsage.java:82)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
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:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:931)
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:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
我很确定它与封闭的方括号有关Firstday
,Lastday
但我不知道如何删除它。任何人都可以在这里帮助我吗?