-2

In the below code I am trying to populate the JSON array with JSON objects (book, and camera).

 [  {
  "camera":{
     "picture":"http:\/\/img7.flixcart.com\/image\-imacyb5emj5yztet.jpeg",
     "model":"Lumix DMC-FP3 Point & Shoot",
     "make":"Panasonic",
     "price":5830
  }
 },
{
  "camera":{
     "picture":"http:\/\/sp.sony-europe.com\/da.jpeg",
     "model":"Digital Still Camera - H Series - DSC-HX200V",
     "make":"Sony",
     "price":510
  }
 },
 {
  "book":{
     "description":"What is self, and how can a",
     "authors":"Douglas R Hofstadter",
     "price":650,
     "id":40724766,
     "title":"G\u00f6del, Escher, Bach: an Eternal Golden Braid"
  }
  },
 {
  "camera":{
     "picture":"http:\/\/www.usa.canon.com\/60d_586x186.gif",
     "model":"Digital SLR Camera EOS 60D",
     "make":"Canon",
     "price":999
  }
  },
   {
  "book":{
     "description":"Tgdfgf fgfg ",
     "authors":"Harold Abelson and Gerald Jay Sussman with Julie Sussman",
     "price":469,
     "id":51087,
     "title":"Structure and Interpretation of Computer Programs"
  }
},

]

public void getDetalFromServer() {
        // TODO Auto-generated method stub

        String  URL="xyz.php";
        HttpClient client= new DefaultHttpClient();
        HttpPost post=new HttpPost(URL);

        try {

            HttpResponse response =client.execute(post);
            HttpEntity getResEntity=response.getEntity();
            String result="";
            if(getResEntity!=null){
                result=EntityUtils.toString(getResEntity);
                System.out.println("result from server: "+result);

                bookDescription=new ArrayList<String>();
                bookAuthors=new ArrayList<String>();
                bookPrice=new ArrayList<String>();
                bookID=new ArrayList<String>();
                bookTitle=new ArrayList<String>();


                responseArray=new JSONArray(result);

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

                    JSONObject bookObject=(JSONObject) responseArray.getJSONObject(i);
                                            bookDescription.add(bookObject.get("description").toString());
                                            bookAuthors.add(bookObject.get("authors").toString());                                              bookPrice.add(bookObject.get("price").toString());

bookID.add(bookObject.get("id").toString()); bookTitle.add(bookObject.get("title").toString());

                }                                       
            }

        } catch (Exception e) {
            // TODO: handle exception
        }
    }

Using this code I am able to just get the first object of the response. Now I am just trying to parse Music object from the response.

Zeina
  • 59
  • 6
Geet taunk
  • 294
  • 1
  • 5
  • 19

1 Answers1

2
             responseArray=new JSONArray(result);
              JSONObject bookObject=null;
               JSONObject json_user1=null;
               JSONObject json_user2=null;
            for(int i=0;i<responseArray.length();i++){

                bookObject= responseArray.getJSONObject(i);
                 if (bookObject.has("book")){
                json_user1 = bookObject.getJSONObject("book");   
                 bookDescription.add(json_user1.getString("description").toString());  
                  bookAuthors.add(json_user1.getString("authors").toString()); 
             }
                else if(bookObject.has("camera")){
                     json_user2 = bookObject.getJSONObject("camera"); 
                     /** Add Camera data in the lists here same as done for book */
               }
          }

Try this.... Inside your json array you have put json objects. so you need to retrieve json objects like book and camera from the array first and then from book object you can get the description and all... :)

Cool Compiler
  • 857
  • 2
  • 9
  • 20
  • i tried and still the same result. its not moving to the next "book" tag in case if its location is 1st n 3rd than its taking first book tag but not able to skip next or 2nd camera tag to reach 3rd book tag. – Geet taunk Jul 12 '12 at 10:55
  • i have again editted the answer.. please try this.. i think i got your problem now.. and this myt work for you – Cool Compiler Jul 12 '12 at 11:28
  • thanks. now it took all book tag values from the response but in the same way its not parsing Camera tag values :( – Geet taunk Jul 12 '12 at 12:03
  • for getting camera values you need to write the else condition.. see again the editted answer – Cool Compiler Jul 12 '12 at 12:26