繁体   English   中英

Java解析JSON for Android

[英]Java parse JSON for android

我正在尝试为我的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

从昨天开始,我一直在尝试。 但是仍然找不到解决方案。

JSONObject jsonObject =new JSONObject();

应该

JSONObject jsonObject =new JSONObject(feed);

您正在使生活更加艰难。

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

然后提取键值对

那是因为您的JSONObject为空

JSONObject jsonObject =new JSONObject();

您必须像这样将respose字符串传递给JSONObject的构造函数

JSONObject jsonObject = new JSONObject(response);

您正在创建一个新的空JSONObject ,它与传入的String无关。您需要new JSONObject(feed)

暂无
暂无

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

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