-1

I'm receiving json data from my php page based on an ID value. The php is fine and I am receiving the JSON data + outputting it onto the textarea I want it in. Except, it comes with it's brackets and table name:

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

How do I make it output only the "TEST TEST TEST" part of my json output instead of all outputting all of it?

my async class

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;


        }

}

editted for clarity

Stevie Wonder
  • 119
  • 1
  • 1
  • 9

2 Answers2

0

You need to parse json for that like if the json is following:

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

This is how your parse 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"));
  }
} 
Aakash
  • 5,181
  • 5
  • 20
  • 37
0

I ended up getting it working. I appreciate the help given by you guys!

 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());
                }
Stevie Wonder
  • 119
  • 1
  • 1
  • 9