简体   繁体   English

尝试使用一个JSONArray解析JSONObject时为空JSON响应

[英]Empty JSON response when trying to parse JSONObject with one JSONArray

I got code that gets JSONArrays , but however when I try to get JSONObject that contains only one JSONArray it gives me empty JSONArray . 我有获取JSONArrays代码,但是,当我尝试获取仅包含一个JSONArray JSONObject ,它给了我空的JSONArray

For example if I need to get data from this JSONObject : 例如,如果我需要从此JSONObject获取数据:

{"events":[{"start":1357714800,"end":1357736400,"name":"Example1","description":""}]}

I get {"events":[]} as JSONObject , [] meaning that it doesn't contain any JSONArrays. 我将{"events":[]}作为JSONObject[]表示它不包含任何JSONArrays。 Also length of JSONObject is in this case 0. But it doesn't throw any kind of Exceptions . 同样,在这种情况下, JSONObject长度为0。但是它不会抛出任何Exceptions

but if JSONObject contains multiple JSONArrays like this: 但是,如果JSONObject包含多个JSONArrays如下所示:

{"events":[{"start":1357714800,"end":1357736400,"name":"Example1","description":""},{"start":1357714600,"end":1357736500,"name":"Example2","description":""},{"start":1357514800,"end":1357536400,"name":"Example3","description":""}]}

then my code works perfect. 那么我的代码就完美了。

Here is the code I use to parse JSON: 这是我用来解析JSON的代码:

private void getObjects(String url) throws JSONException, Exception {
        JSONObject jsonObject = new JSONObject(new NetTask().execute(url).get());
        JSONArray job1 = jsonObject.getJSONArray("events");
        System.out.println(jsonObject.toString());
        System.out.println("JOB1 LENGTH: "+job1.length());
        for (int i = 0; i < job1.length(); i++) {
            JSONObject jsonEvent = job1.getJSONObject(i);
            int start = jsonEvent.getInt("start");
            int end = jsonEvent.getInt("end");
            String name = jsonEvent.getString("name");
            String description = jsonEvent.getString("description");
        }
    }

    public class NetTask extends AsyncTask<String, Integer, String>
    {
        @Override
        protected String doInBackground(String... params)
        {
            String jsonText = "";
            BufferedReader reader = null;
            try {
                URL url = new URL(params[0]);
                reader = new BufferedReader(new InputStreamReader(url.openStream()));
                StringBuffer buffer = new StringBuffer();
                int read;
                char[] chars = new char[1024];
                while ((read = reader.read(chars)) != -1) {
                    buffer.append(chars, 0, read);
                }

                jsonText = buffer.toString();
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
            return jsonText;
        }
    }

is there something wrong that I am missing or is this normal behaviour? 我有什么不对的地方吗?还是这是正常现象?

I tried your given code (though I just made the AsyncTask just return the single-array string, and had to replace the opptunti.getString() stuff with jsonEvent.getString()). 我尝试了给定的代码(尽管我只是使AsyncTask只是返回单数组字符串,并且不得不用jsonEvent.getString()替换opptunti.getString()东西)。 It worked fine, aside from the fact that you're probably blocking the UI thread to wait for the server response. 除了您可能正在阻塞UI线程以等待服务器响应之外,它还可以正常工作。

My guess is the problem is that you are hitting the wrong URL, that the parameters are wrong or something like that. 我的猜测是问题在于您输入了错误的URL,参数错误或类似的东西。

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

相关问题 Java 从 JsonArray 中获取一个 JSONObject 并设置 json 响应 - Java get one JSONObject from a JsonArray and set json response 如何使用GSON WHen对象解析JSON是JSONObject还是JSONArray - How Parse JSON using GSON WHen object is either JSONObject or JSONArray 在Android中解析JSON响应“无法将org.json.JSONObject类型的响应转换为JSONArray” - Parse JSON response in android “response of type org.json.JSONObject cannot be converted to JSONArray” 使用 JSONReader 或 JSONObject/JSONArray 解析 JSON 数据 - Parse JSON data using JSONReader or JSONObject/JSONArray 解析可能是Java中的JSONObject或JSONArray的json字段 - Parse json field that may be an JSONObject or JSONArray in java 如何一次解析JSONObject和JSONArray - How to Parse JSONObject and JSONArray at one time 与邮递员相比,我在响应列表器中得到Empty Json Object。 我如何从JsonObject获取JsonArray作为响应? - I am getting Empty Json Object in response listner as compared to postman. How can i get JsonArray from JsonObject in response? 尝试将JSONArray转换为JSONObject - Trying to convert JSONArray to JSONObject 尝试解析JSONArray时发生ClassCastException - ClassCastException when trying to parse a JSONArray 如果某个元素有时以jsonobject和jsonarray的形式出现,如何解析JSON - How to parse JSON if an element is coming as jsonobject sometime and jsonarray sometime
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM