猿问

Java JSONObject getJsonArray 空字符串

我有一些 JSON 返回如下:


[{"on_arrival_inst":"Ok","order_inst":"Ok","finished_inst":"Ok"},{"on_arrival_inst":"Arrive","order_inst":"Order","finished_inst":"Finished"}]

我试图将它们分成两个数组并按如下方式获取字符串:


jsonResultsObject = new JSONObject(result);

            jsonArray = jsonResultsObject.getJSONArray("");


            int count = 0;

            String onArrive, onReady, onFinished;


            while (count<jsonArray.length()){

                JSONObject JO = jsonArray.getJSONObject(count);

                onArrive = JO.getString("on_arrival_inst");

                onReady = JO.getString("order_inst");

                onFinished = JO.getString("finished_inst");


                System.out.println(onArrive);

                System.out.println(onReady);

                System.out.println(onFinished);


                count++;


            }

但是代码永远不会进入循环,因为数组没有从 JSONObject 填充?


芜湖不芜
浏览 299回答 2
2回答

慕村225694

代码没有进入循环的事实只是因为您JSONArray没有命名的键,""但它包含了JSONObjects。JSON 中的对象和数组具有不同的注释。请参阅:JSON 参考网站所以你的代码应该是:jsonResultsObject = new JSONObject(result);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String onArrive, onReady, onFinished;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i=0;i<jsonArray.length();i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject JO = jsonArray.getJSONObject(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onArrive = JO.getString("on_arrival_inst");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onReady = JO.getString("order_inst");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onFinished = JO.getString("finished_inst");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(onArrive);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(onReady);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(onFinished);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }并且还要确保您的代码在try - catch要捕获的块内JSONException

心有法竹

你的结果是 JSONArray 而不是 JSONObject。这就是为什么您必须将其转换为数组而不是对象的原因。用jsonResultsArray = new JSONArray(result);代替jsonResultsObject = new JSONObject(result);完整的代码将是&nbsp; &nbsp; &nbsp; &nbsp; jsonResultsArray = new JSONArray(result);&nbsp; &nbsp; &nbsp; &nbsp; int count = 0;&nbsp; &nbsp; &nbsp; &nbsp; String onArrive, onReady, onFinished;&nbsp; &nbsp; &nbsp; &nbsp; while (count<jsonResultsArray.length()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject JO = jsonResultsArray.getJSONObject(count);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onArrive = JO.getString("on_arrival_inst");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onReady = JO.getString("order_inst");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onFinished = JO.getString("finished_inst");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(onArrive);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(onReady);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(onFinished);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count++;&nbsp; &nbsp; &nbsp; &nbsp; }
随时随地看视频慕课网APP

相关分类

Java
我要回答