[英]Java parse JSON for android
I am trying to parse Json for my android app, but I couldn't get it work. 我正在尝试为我的Android应用解析Json,但无法正常工作。 I am using org.json library which is port by android. 我正在使用由android移植的org.json库。 I tried different tutorials but I get and exception called JSONException. 我尝试了不同的教程,但得到了名为JSONException的异常。
This is the json response I need to parse. 这是我需要解析的json响应。
{"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. 但是仍然找不到解决方案。
JSONObject jsonObject =new JSONObject();
应该
JSONObject jsonObject =new JSONObject(feed);
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 然后提取键值对
That's because your JSONObject
is empty 那是因为您的JSONObject
为空
JSONObject jsonObject =new JSONObject();
You have to pass respose string to JSONObject
's constructor like this 您必须像这样将respose字符串传递给JSONObject
的构造函数
JSONObject jsonObject = new JSONObject(response);
您正在创建一个新的空JSONObject
,它与传入的String
无关。您需要new JSONObject(feed)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.