I have XML file looking like this
<Reading>
<Item Month="_October.xml">
<kWhReading Firstday="1" />
<kWhReading Lastday="552" />
</Item>
</Reading>
Then using this line of code
JSONArray jsonArray = (JSONArray) JSONSerializer.toJSON( json.toString() );
I got this
[{"@Month":"_October.xml","kWhReading":[{"@Firstday":"1"},{"@Lastday":"552"}]}]
Then, using this
JSONObject jsonobject = (JSONObject) jsonArray.get(0);
I got this
{"@Month":"_October.xml","kWhReading":[{"@Firstday":"1"},{"@Lastday":"552"}]}
Now the problem is, I want to get the value of Lastday
from kWhReading
using
JSONObject readingkWhLast = jsonobject.getJSONObject("kWhReading");
but it doesn't work and it threw exception
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)
I am pretty sure it has to do with the square brackets that enclosed Firstday
and Lastday
but I am not sure how to remove that. Anyone can help me here?
Never used the JSON library, but this should be on the right track
JSONObject readingkWhLast = jsonobject.getJSONArray("kWhReading").get(1);
From your jsonobject
retrieve the JSONArray
named "kWhReading"
and then retrieve the element at offset 1 (the second object).
This is all in the API documentation. You really should step back and read the docs thoroughly so you understand the capabilities of the library.
Once you get jsonArray:
[{"@Month":"_October.xml","kWhReading":[{"@Firstday":"1"},{"@Lastday":"552"}]}]
Then, you need to use getJSONObject() with an index number to get an element within that array. The second one is "kWhReading", so:
JSONObject kWhReadObject = jsonArray.getJSONObject(1);
That should put you here:
{"kWhReading":[{"@Firstday":"1"},{"@Lastday":"552"}]}
To get the value of kWhReading (which is itself an array):
JSONArray kWhReadArray = kWhReadObject.getJSONArray("kWhReading");
Now you're here:
[{"@Firstday":"1"},{"@Lastday":"552"}]
Back to index numbers. Again, you want the second one:
JSONObject lastDayObj = kWhReadArray.getJSONObject(1);
And finally:
String lastDay = lastDayObj.getString("@Lastday");
The docs are here: http://json-lib.sourceforge.net/apidocs/net/sf/json/JSONArray.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.