简体   繁体   English

带有方括号的JSON错误

[英]JSON error with square brackets

I have XML file looking like this 我有看起来像这样的XML文件

<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 现在的问题是,我想使用LastdaykWhReading获取Lastday的值

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. 我很确定这与括在“ Firstday和“ Lastday的方括号有关,但是我不确定如何删除它。 Anyone can help me here? 有人可以在这里帮助我吗?

Never used the JSON library, but this should be on the right track 从未使用过JSON库,但这应该是正确的

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). 从您的jsonobject检索名为"kWhReading"JSONArray ,然后检索偏移量为1的元素(第二个对象)。

This is all in the API documentation. 这些都在API文档中。 You really should step back and read the docs thoroughly so you understand the capabilities of the library. 您确实应该退后一步并仔细阅读文档,以便您了解库的功能。

Once you get jsonArray: 一旦获得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. 然后,您需要使用带有索引号的getJSONObject()来获取该数组中的元素。 The second one is "kWhReading", so: 第二个是“ kWhReading”,因此:

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): 要获取kWhReading的值(它本身是一个数组):

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 这些文档在这里: http : //json-lib.sourceforge.net/apidocs/net/sf/json/JSONArray.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM