2016-11-30 40 views
0

我解析的地方特定格式的谷歌地方api。但是在解析後我得到了空值。我想要的地址格式如下:Mumbai, Maharashtra, IN, 400047。爲此,我將參數typesaddress_components保存到ArrayList中,然後檢查特定的字符串。但是我的價值爲空。如何解析Google Places API的城市,州,國家代碼和Zip詳細信息?

JSONObject jsonObject = new JSONObject(stringBuilder.toString()); 
 
        if (jsonObject.getString("status").equals("OK")) { 
 
         JSONArray array = jsonObject.getJSONArray("results"); 
 
          for(int k=0;k<array.length();k++){ 
 
           JSONObject jObj = array.getJSONObject(0); 
 
           JSONArray jarr = jObj.getJSONArray("address_components"); 
 
           for(int i=0; i<jarr.length();i++){ 
 
            JSONObject jobj = jarr.getJSONObject(i); 
 
            String long_name = jobj.getString("long_name"); 
 
            String short_name = jobj.getString("short_name"); 
 
            JSONArray jarray = jobj.getJSONArray("types"); 
 
            for(int j=0; j<jarray.length();j++){ 
 
             typo.add(jarray.getString(j)); 
 
             if(Arrays.asList(typo).contains("administrative_area_level_2")){ 
 
              cityStr = long_name; 
 
             }else if(Arrays.asList(typo).contains("administrative_area_level_1")){ 
 
              stateStr = long_name; 
 
             }else if(Arrays.asList(typo).contains("country")){ 
 
              countryStr = short_name; 
 
             }else if(Arrays.asList(typo).contains("postal_code")){ 
 
              zipStr = long_name; 
 
             } 
 

 
            } 
 

 

 
           } 
 
          } 
 

 
          result = cityStr + ", " + stateStr + ", " + zipStr + ", " + countryStr; 
 
          
 

 

 
        }

回答

0

我嘗試了許多東西。到底什麼對我來說是optString()。使用optString,我正在檢查數組參數types

JSONObject jsonObject = new JSONObject(stringBuilder.toString()); 
 
        if (jsonObject.getString("status").equals("OK")) { 
 
         JSONArray array = jsonObject.getJSONArray("results"); 
 
          for(int k=0;k<array.length();k++){ 
 
           JSONObject jObj = array.getJSONObject(0); 
 
           JSONArray jarr = jObj.getJSONArray("address_components"); 
 
           for(int i=0; i<jarr.length();i++){ 
 
            JSONObject jobj = jarr.getJSONObject(i); 
 
            String long_name = jobj.getString("long_name"); 
 
            String short_name = jobj.getString("short_name"); 
 
            JSONArray jarray = jobj.getJSONArray("types"); 
 
            for(int j=0; j<jarray.length();j++){ 
 
             String object = jarray.optString(j); 
 
             if(object.equals("administrative_area_level_2")){ 
 
              cityStr = long_name; 
 
              System.out.println("sammy_city "+cityStr); 
 
             } 
 
             if(object.equals("administrative_area_level_1")){ 
 
              stateStr = long_name; 
 
              System.out.println("sammy_state "+stateStr); 
 
             } 
 
             if(object.equals("country")){ 
 
              countryStr = short_name; 
 
              System.out.println("sammy_country "+countryStr); 
 
             } 
 
             if(object.equals("postal_code")){ 
 
              zipStr = long_name; 
 
              System.out.println("sammy_zip "+zipStr); 
 
             } 
 

 
            } 
 

 

 
           } 
 
          } 
 

 
          result = cityStr + ", " + stateStr + ", " + zipStr + ", " + countryStr; 
 
         

相關問題