Java JSON反序列化错误

Java JSON反序列化错误,java,json,deserialization,Java,Json,Deserialization,我有以下使用的代码: 不要担心Widget或WidgetService:这个问题与我如何使用Java JSON API(特别是JSONObject)有关 当我运行上述代码时,我得到: Response is: {"response":{"credits_used":"0.30","job_count":1,"order_id":"243050","currency":"USD"},"opstat":"ok"} Now fetching orderid... Exception in thread

我有以下使用的代码:

不要担心
Widget
WidgetService
:这个问题与我如何使用Java JSON API(特别是
JSONObject
)有关

当我运行上述代码时,我得到:

Response is: {"response":{"credits_used":"0.30","job_count":1,"order_id":"243050","currency":"USD"},"opstat":"ok"}
Now fetching orderid...
Exception in thread "main" org.json.JSONException: JSONObject["order_id"] not found.
    at org.json.JSONObject.get(JSONObject.java:473)
    at org.json.JSONObject.getString(JSONObject.java:654)
    at com.me.myapp.MyDriver.main(MyDriver.java:49)

如您所见,响应中返回了一个order_id字符串字段,其值为“243050”。那么为什么会出现异常呢?

您的
JSONObject响应
指向外部json对象


我很确定,您的响应对象有一个属性
“response”
(和
“opstat”
顺便说一句)包含您期望的对象。

您必须这样做:

response.getJSONObject("response").getString("order_id");

orderid没有直接链接到response对象,它位于另一个ObjectThank@Max Fichtelmann(+1)-那么我需要做什么来修复它呢?应该得到内部响应对象,就像@dstronczak说的那样。
response.getJSONObject("response").getString("order_id");