/** * Produce a JSONArray containing the values of the members of this JSONObject. * * @param names A JSONArray containing a list of key strings. This determines the sequence of the * values in the result. * @return A JSONArray of values. * @throws JSONException If any of the values are non-finite numbers. */ public JSONArray toJSONArray(JSONArray names) throws JSONException { if (names == null || names.length() == 0) { return null; } JSONArray ja = new JSONArray(); for (int i = 0; i < names.length(); i += 1) { ja.put(this.opt(names.getString(i))); } return ja; }
public static String getZipcode(JSONObject data) { try { JSONArray results = data.getJSONArray("results"); JSONObject address = results.getJSONObject(0); JSONArray address_components = address.getJSONArray("address_components"); for (int i = 0; i < address_components.size(); i++) { JSONObject postal_code = address_components.getJSONObject(i); JSONArray types = postal_code.getJSONArray("types"); if (types.getString(0).equals("postal_code")) { return (String) postal_code.get("long_name"); } } return "0"; } catch (NullPointerException npe) { npe.printStackTrace(); } return null; }
/** * Reverse the JSONML transformation, making an XML text from a JSONArray. * * @param ja A JSONArray. * @return An XML string. * @throws JSONException */ public static String toString(JSONArray ja) throws JSONException { int i; JSONObject jo; String key; Iterator<String> keys; int length; Object object; StringBuilder sb = new StringBuilder(); String tagName; String value; // Emit <tagName tagName = ja.getString(0); XML.noSpace(tagName); tagName = XML.escape(tagName); sb.append('<'); sb.append(tagName); object = ja.opt(1); if (object instanceof JSONObject) { i = 2; jo = (JSONObject) object; // Emit the attributes keys = jo.keys(); while (keys.hasNext()) { key = keys.next(); XML.noSpace(key); value = jo.optString(key); if (value != null) { sb.append(' '); sb.append(XML.escape(key)); sb.append('='); sb.append('"'); sb.append(XML.escape(value)); sb.append('"'); } } } else { i = 1; } // Emit content in body length = ja.length(); if (i >= length) { sb.append('/'); sb.append('>'); } else { sb.append('>'); do { object = ja.get(i); i += 1; if (object != null) { if (object instanceof String) { sb.append(XML.escape(object.toString())); } else if (object instanceof JSONObject) { sb.append(toString((JSONObject) object)); } else if (object instanceof JSONArray) { sb.append(toString((JSONArray) object)); } } } while (i < length); sb.append('<'); sb.append('/'); sb.append(tagName); sb.append('>'); } return sb.toString(); }