2015-04-02 131 views
-2

輸出不斷重複每個屬性的名稱。只需要查看一次該名稱,然後可以在單個名稱下爲每個場合打印出prop_name和address。 JSON的樣子:Android JSON for statement JAVA

[{"pmid":"2","name":"CARLYLE MANAGEMENT","result":"1","properties":[{"prop_id":"32","prop_name":"Bonneville Tower","address":"25801 Lakeshore","city":"Euclid","state":"OH","zip":"44132","lat":"41.6223","long":"-81.5034"}]}, 
{"pmid":"1","name":"COMMERCIAL ONE REALTY","result":"18","properties":[{"prop_id":"3","prop_name":"Autumn Chase Apartments","address":"146 Grand Circuit Blvd.","city":"Delaware","state":"OH","zip":"43015","lat":"40.3105","long":"-83.1138"}, 
{"prop_id":"6","prop_name":"Barrington Club Apartments","address":"4600 Barrington Club","city":"Columbus","state":"OH","zip":"43220","lat":"40.0536","long":"-83.0448"},{"prop_id":"17","prop_name":"Battleship Building Condominium","address":"444 North Front St.","city":"Columbus","state":"OH","zip":"43215","lat":"39.9712","long":"-83.0042"}] 

代碼當前:

String pmid = mJsonObject.getString("pmid"); 
       String name = mJsonObject.getString("name"); 
       String result = mJsonObject.getString("result"); 

        JSONArray mJsonArrayProperty = mJsonObject.getJSONArray("properties"); 
        int innerLength = mJsonArrayProperty.length(); 
        for (int k = 0; k < innerLength; k++) { 
       // JSONArray mJsonArrayProperty = mJsonObject.getJSONArray("properties"); 
       // for (int i = 0; i < mJsonArrayProperty.length(); i++) { 
        JSONObject mJsonObjectProperty = mJsonArrayProperty.getJSONObject(k); 

        String prop_id = mJsonObjectProperty.getString("prop_id"); 
        String prop_name = mJsonObjectProperty.getString("prop_name"); 
        String address = mJsonObjectProperty.getString("address"); 
        String city = mJsonObjectProperty.getString("city"); 
        String state = mJsonObjectProperty.getString("state"); 
        String zip = mJsonObjectProperty.getString("zip"); 
        String lat = mJsonObjectProperty.getString("lat"); 
        String lon = mJsonObjectProperty.getString("long"); 

        // tmp hashmap for single contact 

         HashMap<String, String> contact = new HashMap<String, String>(); 
        // adding each child node to HashMap key => value 
        contact.put(TAG_EMAIL, prop_name); 
         contact.put(TAG_NAME, name); 
        contact.put(TAG_PHONE, address); 


        // adding contact to contact list 
        contactList.add(contact); 
       } 

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

輸出給我prop_name和地址,但它也表示,在名稱,每次過來。目標是一次查看該名稱,然後查看其下的多個屬性。這怎麼可以用for或if語句或其他方式來完成。

+0

是不是使用'CustomAdapter'? – 2015-04-02 06:10:01

+0

是你的'mJsonObject'是一個數組? – BBdev 2015-04-02 06:30:35

+0

是BBDev它是一個數組 – 2015-04-08 20:06:45

回答

0

我剛寫的算法中,你可以怎麼做呢?

  • 首先,你必須得到您擁有jsondataJSONObject。因爲整個jsondata你是一個數組。
  • 現在從JSONArray你應該得到的字符串pmid,名稱等。
  • 現在再次,你必須得到JSONArray因爲propertiesJSONArray現在你可以重複它。

爲此,您應該使用兩個for循環或您想要使用的任何迭代方法。

0
Try Out this... 

    JSONArray one=new JSONArray(json); 
     for (int i = 0; i < one.length(); i++) 
    { 
     JSONObject jsonObject=one.getJSONObject(i); 
     String pmid=jsonObject.getString("pmid"); 
     String name=jsonObject.getString("name"); 
     String result=jsonObject.getString("result"); 
     JSONArray properties=jsonObject.getJSONArray("properties"); 
     for (int j = 0; j < properties.length(); j++) 
     { 
      JSONObject js=properties.getJSONObject(j); 
      String prop_id=js.getString("prop_id"); 
      String prop_name=js.getString("prop_name"); 
      String address=js.getString("address"); 
      String city=js.getString("city"); 
      String state=js.getString("state"); 
      String zip=js.getString("zip"); 
      String lat=js.getString("lat"); 
      String Long=js.getString("long"); 
     } 
    } 
+0

這個仍然顯示名稱每次屬性被列出 – 2015-04-02 12:13:57