-1

I want to read each element and store each json element in an arraylist efficiently from the json format data below

   getData =  {"block_id":"2","body":"<p>json data" }


        String id = getData.getString("block_id");
        String name = getData.getString("body");
Dimitri
  • 677
  • 3
  • 19
  • 46
  • Your data is very less to predict anything. – Tanmay Mandal Jun 04 '13 at 10:14
  • There is a missing quote in the JSON which makes it invlaid. and are you looking for a solution specific to this JSON data.? – SKK Jun 04 '13 at 10:15
  • i want to retrieve block_id and body and store it in an arraylist – Dimitri Jun 04 '13 at 10:17
  • first get json data in separate and then store it into arraylist.. – Sagar Maiyad Jun 04 '13 at 10:17
  • http://www.androidhive.info/2012/01/android-json-parsing-tutorial/ just check out this link and try to understand how JSON parsing works. you could get answers for these type of questions in google without downvotes and criticism. – SKK Jun 04 '13 at 10:18

3 Answers3

0

You may use following code which retreiving json data

HttpParams httpParams = new BasicHttpParams(); HttpClient client = new DefaultHttpClient(httpParams);

         String url = your url;

         Log.v(TAG, "URL In onPreExecute"+url);
         HttpPost request = new HttpPost(url);

try 
        {
            response = client.execute(request);
             HttpEntity entity = response.getEntity();
             ent=entity.toString();
              if(entity!=null)
              {
                  InputStream instream = entity.getContent();
                  BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
                  builder = new StringBuilder();
                  String line = null;
                  while((line=reader.readLine())!=null)
                  {
                      builder.append(line);
                  }
                  instream.close();
                  Log.v("CMH", "Result of http " + builder.toString());
                }
                else
                {
                     Log.v("Data", "Failed to download file");
                }


              JSONObject jsonObject = new JSONObject(builder.toString());
                //      String csSuccess = jsonObject.getString("vidlist");
                        jsonarray = jsonObject.getJSONArray("vidlist");
                        NumOfObjects  = jsonarray.length();
                        Log.v(TAG, "NumOfObjects "+NumOfObjects);

                for(int Video = 0; Video < NumOfObjects; Video++)
                    {
                        SetterGetter setget = new SetterGetter();

                        video_obj = jsonarray.getJSONObject(Video);


                    String id = video_obj.getString("video_id"));


                    }
MALIXX
  • 1
  • 1
0

ArrayList declaration,

private ArrayList<Integer> idList = new ArrayList<Integer>();
        private ArrayList<String> nameList = new ArrayList<String>();

Once you successfully parsed the data,

 String id = getData.getString("block_id");
  String name = getData.getString("body");

  //Here added into arraylist...

  idList.add(id);
  nameList.add(name);
Aerrow
  • 12,086
  • 10
  • 56
  • 90
0

You can try below code

JSONObject json = new JSONObject(jsonstring);

String id = json.getString("block_id");
String name = json.getString("body");

ArrayList<String> arrIds = new ArrayList<String>();
arrIds.add(id);
Nirali
  • 13,571
  • 6
  • 40
  • 53