Java 为 Android 解析 JSON

标签 java android json

我正在尝试为我的 Android 应用程序解析 Json,但我无法让它工作。我正在使用由 android 移植的 org.json 库。我尝试了不同的教程,但我得到了一个名为 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

我从昨天开始就在尝试这个。但仍然找不到解决方案。

最佳答案

你让你的生活变得更加艰难。

public void extractJsonData(String feed){         
feed = feed.trim(); 
JSONObject json = new JSONObject(feed);

然后提取键值对

关于Java 为 Android 解析 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11420659/

相关文章:

java - 应用关闭时通知仍然出现

python - json 模块中的 object_hook 似乎没有按预期工作

json - Go lang( postman )中的请求正文为空

java - 始终为同一客户创建 QuickBooks Online Java SDK 发票

java - 如何将已使用 borderlayout 布局的面板中的对象居中

java - 组合 URL 路径

java - 可以使用特定按钮振动另一个按钮吗?如何?

java - 地理位置谷歌地图 v2

Delphi 中的 JSON 数组

java - 从 fragment 访问 Activity 的最佳方式