2

I am getting this JSON object from a PHP file on internet:

{
"1": "bar",
"2": "foo",
"3": "umIar",
"4": "shubham"
}

I tried a lot but was unable to convert it to a String array. How to do it?

I used this JAVA code :

public class showNewsFeed extends AsyncTask <String, Void, String> {
        HttpClient httpclient = new DefaultHttpClient();

         HttpPost httppost = new HttpPost("http://www.ludlowcastle.co.in/getNotifiedDTU/newsFeed.php");
         String result;
         protected String doInBackground(String... voids ){
             try {
                 HttpResponse response = httpclient.execute(httppost);
                 HttpEntity entity = response.getEntity();
                 InputStream is = entity.getContent();
                 String result = convertStreamToString(is);
                 System.out.println("String response = " + result);
             } catch (Exception e){
                 e.printStackTrace();
             }

             return result;
         }
        protected void onProgressUpdate(Void...voids){

        }


        protected void onPostExecute(String result){
            super.onPostExecute(result);

            try {
                JSONObject obj=new JSONObject(result);
                Iterator iter = obj.keys();
                 while(iter.hasNext()){
                   String key = (String)iter.next();
                   System.out.println(obj.getString(key));
                }

              } catch (Exception e) {
                e.printStackTrace();
              }
        }



    }

This is the logcat :

02-18 04:58:19.412: W/System.err(1268): java.lang.NullPointerException
02-18 04:58:19.431: W/System.err(1268):     at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
02-18 04:58:19.431: W/System.err(1268):     at org.json.JSONTokener.nextValue(JSONTokener.java:94)
02-18 04:58:19.431: W/System.err(1268):     at org.json.JSONObject.<init>(JSONObject.java:154)
02-18 04:58:19.431: W/System.err(1268):     at org.json.JSONObject.<init>(JSONObject.java:171)
02-18 04:58:19.431: W/System.err(1268):     at com.umair.shubham.fragment1$showNewsFeed.onPostExecute(fragment1.java:71)
02-18 04:58:19.431: W/System.err(1268):     at com.umair.shubham.fragment1$showNewsFeed.onPostExecute(fragment1.java:1)
02-18 04:58:19.441: W/System.err(1268):     at android.os.AsyncTask.finish(AsyncTask.java:631)
02-18 04:58:19.441: W/System.err(1268):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
02-18 04:58:19.441: W/System.err(1268):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
02-18 04:58:19.451: W/System.err(1268):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-18 04:58:19.451: W/System.err(1268):     at android.os.Looper.loop(Looper.java:137)
02-18 04:58:19.451: W/System.err(1268):     at android.app.ActivityThread.main(ActivityThread.java:5039)
02-18 04:58:19.451: W/System.err(1268):     at java.lang.reflect.Method.invokeNative(Native Method)
02-18 04:58:19.451: W/System.err(1268):     at java.lang.reflect.Method.invoke(Method.java:511)
02-18 04:58:19.451: W/System.err(1268):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-18 04:58:19.461: W/System.err(1268):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-18 04:58:19.471: W/System.err(1268):     at dalvik.system.NativeStart.main(Native Method)
omerjerk
  • 4,090
  • 5
  • 40
  • 57
  • Possible duplicate of: [{1}](http://stackoverflow.com/q/7551305/1438733), [{2}](http://stackoverflow.com/q/6356231/1438733), [{3}](http://stackoverflow.com/q/4604657/1438733), and [numerous others](https://www.google.com/search?hl=en&q=android+json+to+string+array). – Cat Feb 18 '13 at 04:45
  • 1
    add [this](http://stackoverflow.com/questions/4819405/json-formatted-string-to-string-array) and [this too](http://stackoverflow.com/questions/9708918/java-parse-json-string-to-array-or-objectlist) – Rahul Feb 18 '13 at 04:49
  • just replace this line String result = convertStreamToString(is); with result = convertStreamToString(is); in do in background – Khan Feb 18 '13 at 05:05

1 Answers1

1

Try this

ArrayList<String> arr=new ArrayList<String>();
JSONObject obj=new JSONObject(response);
Iterator iter = obj.keys();
 while(iter.hasNext()){
   String key = (String)iter.next();
   arr.add(obj.getString(key));
}
koti
  • 3,681
  • 5
  • 34
  • 58