0

My JSON array is this:

{"Name_1":1,"Name_2":0,"Name_3":0}

and my code in java for getting the values and store them in a separate array is the following:

int[] operations= new int[3];
             String result = "";
             InputStream is = null;
             StringBuilder sb=null;
                try{
                    HttpClient httpclient = new DefaultHttpClient();

                    HttpPost httppost = new HttpPost("http://testteamgr.netau.net/parsing/test.php");
                    HttpResponse response = httpclient.execute(httppost);
                    HttpEntity entity = response.getEntity();
                    is = entity.getContent();
            }catch(Exception e){
                    Log.e("log_tag", "Error in http connection "+e.toString());
            }
            //convert response to string
            try{
                    BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                    sb = new StringBuilder();
                    String line = null;
                    while ((line = reader.readLine()) != null) {
                            sb.append(line + "\n");
                    }
                    is.close();

                    result=sb.toString();
            }catch(Exception e){
                    Log.e("log_tag", "Error converting result "+e.toString());
            }
            try{

                JSONObject json_data = new JSONObject(result);
                System.out.println("Length of json is"+jArray.length());
                for(int i=0;i<jArray.length();i++){

                       if (i==0) operations[0]=json_data.getInt("Name_1");
                       else if (i==1) operations[1]=json_data.getInt("Name_2");
                       else if (i==2) operations[2]=json_data.getInt("Name_3"); }

and I am getting those errors:

value br of type java.lang.string cannot be converted to jsonobject

If I print out the result I do not see the JSONobject but the html code.

So what I want is to get these 3 values into a separate array.

4
  • 1
    I think it's because you ask an array whereas you just got a object. In JSON, arrays are wrapped wetween [ and ], you've got { and }, that means an object. Commented Aug 10, 2012 at 18:33
  • code edited but problem remains Commented Aug 10, 2012 at 18:37
  • which Log gives you this error? Commented Aug 10, 2012 at 18:39
  • code is the above. If I add a println of the result, after result=sb.toString I see html being printed out. Commented Aug 10, 2012 at 18:42

1 Answer 1

1

You have an object, not an array. To process the result you can use following code:

    String json = "{\"Name_1\":1,\"Name_2\":0,\"Name_3\":0}";
    JSONObject object = new JSONObject(json);
    String[] propertyNames = JSONObject.getNames(object);
    String[] values = new String[propertyNames.length];
    for (int i = 0; i < propertyNames.length; i++) {
        values[i] = String.valueOf(object.get(propertyNames[i]));
    }
Sign up to request clarification or add additional context in comments.

2 Comments

what if the String json is not static? As you can see is code that I fetch from a php script.
I used that just for example, try to use your fetched value instead, it should work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.