【问题标题】:Cannot parse json object from StringBuilder android无法从 StringBuilder android 解析 json 对象
【发布时间】:2016-03-23 12:57:43
【问题描述】:

我试图从 String builder 解析 json,但它什么也没有。这是我的代码

private class GetClass extends AsyncTask<String, Void, Void> {

        private final Context context;

        public GetClass(Context c){
            this.context = c;
        }

        protected void onPreExecute(){
            progress= new ProgressDialog(this.context);
            progress.setMessage("Loading");
            progress.show();
        }

        @Override
        protected Void doInBackground(String... params) {
            try {

                final TextView outputView = (TextView) findViewById(R.id.showOutput);
                URL url = new URL("my_url");

                HttpURLConnection connection (HttpURLConnection)url.openConnection();
                connection.setRequestMethod("GET");
                connection.setRequestProperty("USER-AGENT", "Mozilla/5.0");
                connection.setRequestProperty("ACCEPT-LANGUAGE", "en-US,en;0.5");

                int responseCode = connection.getResponseCode();

                //System.out.println("\nSending 'POST' request to URL : " + url);
                //System.out.println("Post parameters : " + urlParameters);
                //System.out.println("Response Code : " + responseCode);

                final StringBuilder output = new StringBuilder();
                //output.append(System.getProperty("line.separator") + "Request Parameters " + urlParameters);
                //output.append(System.getProperty("line.separator")  + "Response Code " + responseCode);
                //output.append(System.getProperty("line.separator")  + "Type " + "GET");
                BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String line = "";
                StringBuilder responseOutput = new StringBuilder();
                //System.out.println("output===============" + br);
                while((line = br.readLine()) != null ) {
                    responseOutput.append(line);
                }
                br.close();



                output.append(responseOutput.toString());

                UsersActivity.this.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            JSONObject mainObject = new JSONObject(output.toString());
                            JSONObject id = mainObject.getJSONObject("id");

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        outputView.setText(output);
                        progress.dismiss();

                    }
                });


            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }

//        protected void onPostExecute() {
//            progress.dismiss();
//        }

    }

===============================================

这是我的 json 数据

[{"id":4,"name":"test","email":"test@test.com","approved":1,"admin":0,"responsible":"test" ,"标志":"","总统":"测试","成员":50,"使命":"测试","地址":"我是 测试 地址","电话":"12345678","网站":"http://test.com","facebook":"http://facebook.com/test","created_at":"2016-03- 17 11:45:03","updated_at":"2016-03-22 19:47:47"},{"id":5,"name":"test","email":"contact@test.tn","approved":1,"admin":1,"responsible" :"","logo":null,"总统":null,"members":null,"使命":null,"address":null,"tel":null,"website":null,"facebook":空,“created_at”:“2016-03-19 08:24:50","updated_at":"2016-03-22 19:48:25"},{"id":7,"name":"test","email":"test@test.tn","approved":1,"admin":0,"responsible" :"","logo":null,"总统":null,"members":null,"使命":null,"address":null,"tel":null,"website":null,"facebook":空,“created_at”:“2016-03-19 11:30:07","updated_at":"2016-03-22 19:46:10"},{"id":11,"name":"test","email":"test@gmail.com","approved":1,"admin":0,"responsible" :"","logo":null,"总统":null,"members":null,"使命":null,"address":null,"tel":null,"website":null,"facebook":空,“created_at”:“2016-03-21 15:52:18","updated_at":"2016-03-23 09:57:55"}]

【问题讨论】:

  • “但它什么也没有。”意义?你想用你的 JSONObject 实现什么(你的 json 是一个列表而不是带有“id”的对象)?
  • 这是一个故事,你有什么问题?
  • 为什么要在 UI 线程上解析大量数据???

标签: android


【解决方案1】:

JSON 响应是 JSONArray 而不是 JSONObject。 就这样吧,

JSONArray mainObject = new JSONArray(output.toString());

for (int i = 0; i < mainObject.length(); i++) {
    JSONObject object = mainObject.getJSONObject(i);
    int id = object.getInt("id");  //This will have the value of the id
}

【讨论】:

    【解决方案2】:

    您要做的第一件事是 getJSONArray,因为您显示的结果有一个数组作为根元素,而不是 JSONObject。然后需要遍历数组,获取每个JSONObject:

    JSONArray values= new JSONArray(output.toString());
    for (int i = 0; i < values.size(); i++) {
    
                   JSONObject mainObject= values.getJSONObject(i); 
                   int id = mainObject.getInt("id");
    }
    

    【讨论】:

      猜你喜欢
      • 2015-01-25
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多