我正在尝试为我的 android 应用程序解析 Json,但我无法让它工作。我正在使用 org.json 库,它是 android 的端口。我尝试了不同的教程,但我得到了一个名为 JSONException 的异常。
这是我需要解析的 json 响应。
{"status":0,"id":"8bb90729f836f30d179689b01f60fb41-1","hypotheses":[{"utterance":"this is a simple test to see if it's working","confidence":0.88661164}]}
这是我用来解析它的代码。
public void extractJsonData(String feed){
feed = feed.trim();
feed = feed.trim();
try {
JSONObject jsonObject =new JSONObject();
Log.i(GetJSon.class.getName(), jsonObject.getString("status"));
Log.i(GetJSon.class.getName(), jsonObject.getString("id"));
Log.i(GetJSon.class.getName(), jsonObject.getString("hypotheses"));
gettting the array of hypotheses
JSONObject hypotheses = jsonObject.getJSONObject("hypotheses");
Log.i(GetJSon.class.getName(), hypotheses.getString("utterance"));
//Log.i(GetJSon.class.getName(), hypotheses.getString("confidence"));
//}
} catch (Exception e) {
e.printStackTrace();
}
}
这是我得到的错误。
07-09 15:54:38.564: I/global(8719): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
07-09 15:54:38.564: I/Json Response(8719): {"status":0,"id":"8bb90729f836f30d179689b01f60fb41-1","hypotheses":[{"utterance":"this is a simple test to see if it's working","confidence":0.88661164}]}
07-09 15:54:38.564: W/System.err(8719): org.json.JSONException: JSONObject["status"] not found.
07-09 15:54:38.564: W/System.err(8719): at org.json.JSONObject.get(JSONObject.java:287)
07-09 15:54:38.564: W/System.err(8719): at com.isuru.recordapp.GetJSon.extractJsonData(GetJSon.java:19)
07-09 15:54:38.564: W/System.err(8719): at com.isuru.recordapp.Main$7.onClick(Main.java:253)
07-09 15:54:38.564: W/System.err(8719): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:158)
07-09 15:54:38.564: W/System.err(8719): at android.os.Handler.dispatchMessage(Handler.java:99)
07-09 15:54:38.564: W/System.err(8719): at android.os.Looper.loop(Looper.java:123)
07-09 15:54:38.564: W/System.err(8719): at android.app.ActivityThread.main(ActivityThread.java:4363)
07-09 15:54:38.564: W/System.err(8719): at java.lang.reflect.Method.invokeNative(Native Method)
07-09 15:54:38.574: W/System.err(8719): at java.lang.reflect.Method.invoke(Method.java:521)
07-09 15:54:38.574: W/System.err(8719): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-09 15:54:38.574: W/System.err(8719): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-09 15:54:38.574: W/System.err(8719): at dalvik.system.NativeStart.main(Native Method)
07-09 15:54:38.574: I/NotificationService(57): enqueueToast pkg=com.isuru.recordapp callback=android.app.ITransientNotification$Stub$Proxy@44d97440 duration=1
07-09 15:54:38.644: W/InputManagerService(57): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@44e09d28
我从昨天开始就在尝试这个。但还是找不到解决办法。