1

I am trying to parse Json for my android app, but I couldn't get it work. I am using org.json library which is port by android. I tried different tutorials but I get and exception called JSONException.

This is the json response I need to parse.

{"status":0,"id":"8bb90729f836f30d179689b01f60fb41-1","hypotheses":[{"utterance":"this is a simple test to see if it's working","confidence":0.88661164}]}

And this is the code I use to parse it.

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();
        }
    }

Here is the error I get.

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

I am trying this since yesterday. But still couldn't find a solution.

1
  • JSONObject jsonObject =new JSONObject(); .. Commented Jul 10, 2012 at 19:46

4 Answers 4

4

You are making your life much harder.

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

then extract the keyvalue pairs

Sign up to request clarification or add additional context in comments.

Comments

4
JSONObject jsonObject =new JSONObject();

should be

JSONObject jsonObject =new JSONObject(feed);

Comments

1

That's because your JSONObject is empty

JSONObject jsonObject =new JSONObject();

You have to pass respose string to JSONObject's constructor like this

JSONObject jsonObject = new JSONObject(response);

Comments

0

You are creating a new empty JSONObject that has nothing to do with the String you passed in. You need new JSONObject(feed)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.