2012-07-12 75 views
-1

我已经尝试了很多,但可以解析这个JSON。我需要数组中的所有abjects(书和照相机)值。谁能帮我?如何制作CaremaOjects值和bookObject值的数组。如何解析这个JSON响应与不同的对象?

[ { 
    "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" 
    } 
}, 

] 

我使用的代码被 公共无效getDetalFromServer(){// TODO自动生成方法存根

 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( “标题”)的toString());

   }          
      } 

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

使用此代码,我能够只得到响应的第一个对象。(仪式现在我只是想从响应解析音乐对象,为什么我没有处理)感谢

+0

您可以将此链接用于JSON解析http://stackoverflow.com/a/11446076/1441666 – Nirali 2012-07-12 08:55:28

+0

感谢您的评论,但该链接没有帮助:( – 2012-07-12 09:00:08

+0

请使用下面所以答案的链接来解析这种类型的JSon文件。[如何使用Json解析?](http://stackoverflow.com/questions/8850213/how-to-use-json-parsing) – 2012-07-12 07:59:42

回答

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 */ 
       } 
      } 

尝试这....在你的json数组中,你已经把json对象。所以你需要首先从数组中检索书籍和相机等json对象,然后从书籍对象中获取描述和所有... :)

+0

好的。我试图感谢 – 2012-07-12 10:13:25

+0

它不工作:(它没有移动到下一个标签 – 2012-07-12 10:30:24

+0

我试过了,但仍然是相同的结果。如果它的位置是第1位,它不会移动到下一个“书”标签n第3位比第1位书标签,但不能跳过下一个或第二个相机标签,以达到第三个书签 – 2012-07-12 10:55:01