正确接收JSON数据

时间:2015-10-02 15:47:19

标签: android json

我正在根据ID值从我的php页面接收json数据。 PHP很好,我收到JSON数据+输出到我想要的textarea。除了,它带有括号和表名:

[{"analysis":TEST TEST TEST TEST}]

如何让它仅输出我的json输出的“TEST TEST TEST”部分而不是全部输出?

我的异步类

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


        @Override
        protected String doInBackground(String... params) {
            // Create a new HttpClient and Post Header
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://192.168.0.3/ex/match_analysis.php");


            String jsonResult = "";
            try {
                // Add your data
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);

                nameValuePairs.add(new BasicNameValuePair("ID", passedID.toString()));
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                // Execute HTTP Post Request
                HttpResponse response = httpclient.execute(httppost);

                HttpEntity entity = response.getEntity();
                InputStream webservice = entity.getContent();

                try {
                    BufferedReader reader = new BufferedReader(new InputStreamReader(webservice, "iso-8859-1"), 8);

                    analysisView.setText("" + reader.readLine());
                    webservice.close();
                } catch (Exception e) {
                    Log.e("log_tag", "error converting result " + e.toString());
                }


                //   jsonResult = response.getEntity().getContent().toString();
                // passedView.append("" + jsonResult.toString());
                //System.out.println(jsonResult.toString());
            } catch (IOException e) {

            }
            return null;


        }

}

为了清晰起见而编辑

2 个答案:

答案 0 :(得分:0)

你需要解析json,就像json遵循:

[{"analysis":"abc","key2":"value"}]

这是你解析json的方式

//your jsonResult
JSONArray json_array=new JSONArray(jsonResult.toString());
for(int i=0;i< json_array.length ;i++){
 if(json_array.getJSONObject(i).has("analysis")){
  System.out.println(json_array.getJSONObject(i).getString("analysis"));
  }
if(json_array.getJSONObject(i).has("key2")){
  System.out.println(json_array.getJSONObject(i).getString("key2"));
  }
} 

答案 1 :(得分:0)

我最终得到了它的工作。我感谢你们的帮助!

 BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
                   StringBuilder builder = new StringBuilder();
                    for(String line = null; (line = reader.readLine()) !=null;){
                        builder.append(line).append("\n");
                    }
                    JSONTokener tokener = new JSONTokener(builder.toString());
                    JSONArray finalAnswer = new JSONArray(tokener);

                    for(int i = 0; i < finalAnswer.length(); i++) {

                        JSONObject jsonChildNode;

                        try {


                            jsonChildNode = finalAnswer.getJSONObject(i);

                            String analysis = jsonChildNode.getString("analysis");

                            analysisView.setText(analysis.toString());
                        } catch (Exception e) {

                        }
                    }
                }
                    catch (Exception e) {
                    Log.e("log_tag", "error converting result " + e.toString());
                }