List of usage examples for org.json JSONArray length
public int length()
From source file:com.jennifer.ui.util.DomUtil.java
public String collapseAttr() { StringBuilder str = new StringBuilder(); String style = collapseStyle(); if (!style.equals("")) { if (attrs.has("style")) { style = getString("style") + ";" + style; }/* ww w. j a va2 s . c o m*/ put("style", style); } JSONArray names = attrs.names(); if (names != null) { for (int i = 0, len = names.length(); i < len; i++) { String key = names.getString(i); String value = get(key).toString(); str.append(" " + key + "=\"" + value + "\""); } } return str.toString(); }
From source file:com.jennifer.ui.util.DomUtil.java
public static String join(JSONArray list, String separator) { int len = list.length(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < len; i += 1) { if (i > 0) { sb.append(separator);//from ww w. j a v a2 s .co m } sb.append(list.getString(i)); } return sb.toString(); }
From source file:com.jennifer.ui.util.DomUtil.java
public DomUtil attr(JSONObject JSONObject) { JSONArray list = JSONObject.names(); for (int i = 0, len = list.length(); i < len; i++) { String key = list.getString(i); put(key, JSONObject.get(key));//from w w w.j a va2 s . c o m } return this; }
From source file:edu.msu.walajahi.sunshine.app.FetchWeatherTask.java
/** * Take the String representing the complete forecast in JSON Format and * pull out the data we need to construct the Strings needed for the wireframes. * * Fortunately parsing is easy: constructor takes the JSON string and converts it * into an Object hierarchy for us.//w ww . j a va 2 s .c om */ private void getWeatherDataFromJson(String forecastJsonStr, String locationSetting) throws JSONException { // Now we have a String representing the complete forecast in JSON Format. // Fortunately parsing is easy: constructor takes the JSON string and converts it // into an Object hierarchy for us. // These are the names of the JSON objects that need to be extracted. // Location information final String OWM_CITY = "city"; final String OWM_CITY_NAME = "name"; final String OWM_COORD = "coord"; // Location coordinate final String OWM_LATITUDE = "lat"; final String OWM_LONGITUDE = "lon"; // Weather information. Each day's forecast info is an element of the "list" array. final String OWM_LIST = "list"; final String OWM_PRESSURE = "pressure"; final String OWM_HUMIDITY = "humidity"; final String OWM_WINDSPEED = "speed"; final String OWM_WIND_DIRECTION = "deg"; // All temperatures are children of the "temp" object. final String OWM_TEMPERATURE = "temp"; final String OWM_MAX = "max"; final String OWM_MIN = "min"; final String OWM_WEATHER = "weather"; final String OWM_DESCRIPTION = "main"; final String OWM_WEATHER_ID = "id"; try { JSONObject forecastJson = new JSONObject(forecastJsonStr); JSONArray weatherArray = forecastJson.getJSONArray(OWM_LIST); JSONObject cityJson = forecastJson.getJSONObject(OWM_CITY); String cityName = cityJson.getString(OWM_CITY_NAME); JSONObject cityCoord = cityJson.getJSONObject(OWM_COORD); double cityLatitude = cityCoord.getDouble(OWM_LATITUDE); double cityLongitude = cityCoord.getDouble(OWM_LONGITUDE); long locationId = addLocation(locationSetting, cityName, cityLatitude, cityLongitude); // Insert the new weather information into the database Vector<ContentValues> cVVector = new Vector<ContentValues>(weatherArray.length()); // OWM returns daily forecasts based upon the local time of the city that is being // asked for, which means that we need to know the GMT offset to translate this data // properly. // Since this data is also sent in-order and the first day is always the // current day, we're going to take advantage of that to get a nice // normalized UTC date for all of our weather. Time dayTime = new Time(); dayTime.setToNow(); // we start at the day returned by local time. Otherwise this is a mess. int julianStartDay = Time.getJulianDay(System.currentTimeMillis(), dayTime.gmtoff); // now we work exclusively in UTC dayTime = new Time(); for (int i = 0; i < weatherArray.length(); i++) { // These are the values that will be collected. long dateTime; double pressure; int humidity; double windSpeed; double windDirection; double high; double low; String description; int weatherId; // Get the JSON object representing the day JSONObject dayForecast = weatherArray.getJSONObject(i); // Cheating to convert this to UTC time, which is what we want anyhow dateTime = dayTime.setJulianDay(julianStartDay + i); pressure = dayForecast.getDouble(OWM_PRESSURE); humidity = dayForecast.getInt(OWM_HUMIDITY); windSpeed = dayForecast.getDouble(OWM_WINDSPEED); windDirection = dayForecast.getDouble(OWM_WIND_DIRECTION); // Description is in a child array called "weather", which is 1 element long. // That element also contains a weather code. JSONObject weatherObject = dayForecast.getJSONArray(OWM_WEATHER).getJSONObject(0); description = weatherObject.getString(OWM_DESCRIPTION); weatherId = weatherObject.getInt(OWM_WEATHER_ID); // Temperatures are in a child object called "temp". Try not to name variables // "temp" when working with temperature. It confuses everybody. JSONObject temperatureObject = dayForecast.getJSONObject(OWM_TEMPERATURE); high = temperatureObject.getDouble(OWM_MAX); low = temperatureObject.getDouble(OWM_MIN); ContentValues weatherValues = new ContentValues(); weatherValues.put(WeatherEntry.COLUMN_LOC_KEY, locationId); weatherValues.put(WeatherEntry.COLUMN_DATE, dateTime); weatherValues.put(WeatherEntry.COLUMN_HUMIDITY, humidity); weatherValues.put(WeatherEntry.COLUMN_PRESSURE, pressure); weatherValues.put(WeatherEntry.COLUMN_WIND_SPEED, windSpeed); weatherValues.put(WeatherEntry.COLUMN_DEGREES, windDirection); weatherValues.put(WeatherEntry.COLUMN_MAX_TEMP, high); weatherValues.put(WeatherEntry.COLUMN_MIN_TEMP, low); weatherValues.put(WeatherEntry.COLUMN_SHORT_DESC, description); weatherValues.put(WeatherEntry.COLUMN_WEATHER_ID, weatherId); cVVector.add(weatherValues); } int inserted = 0; // add to database if (cVVector.size() > 0) { // Student: call bulkInsert to add the weatherEntries to the database here ContentValues[] cvArray = new ContentValues[cVVector.size()]; cVVector.toArray(cvArray); inserted = mContext.getContentResolver().bulkInsert(WeatherEntry.CONTENT_URI, cvArray); } Log.d(LOG_TAG, "FetchWeatherTask Complete. " + inserted + " Inserted"); } catch (JSONException e) { Log.e(LOG_TAG, e.getMessage(), e); e.printStackTrace(); } }
From source file:com.orange.oidc.secproxy_service.Token.java
public void fromToken(String token) { reset();//from w w w . j av a 2s . co m if (token == null) return; try { JSONObject jObject = null; // try token as is try { jObject = new JSONObject(token); } catch (Exception e) { } // try to decode JWT if (jObject == null) { String ds = getJSON(token); if (ds != null) jObject = new JSONObject(ds); } if (jObject != null) { JSONArray names = jObject.names(); if (names != null) { for (int j = 0; j < names.length(); j++) { String name = names.getString(j); // Log.d("Token",name); setField(name, jObject.get(name)); } } } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:com.orange.oidc.secproxy_service.Token.java
void setField(String name, Object object) { if (name == null || name.length() == 0) return;// w ww. j a v a 2 s . co m if (name.compareTo("iss") == 0) { iss = (String) object; } else if (name.compareTo("sub") == 0) { sub = (String) object; } else if (name.compareTo("aud") == 0) { JSONArray jArray = (JSONArray) object; aud = ""; for (int i = 0; i < jArray.length(); i++) { try { aud += jArray.getString(i) + " "; } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } else if (name.compareTo("jti") == 0) { jti = (String) object; } else if (name.compareTo("exp") == 0) { exp = getIntAsString(object); } else if (name.compareTo("iat") == 0) { iat = getIntAsString(object); } else if (name.compareTo("auth_time") == 0) { auth_time = getIntAsString(object); } else if (name.compareTo("nonce") == 0) { nonce = (String) object; } else if (name.compareTo("at_hash") == 0) { at_hash = (String) object; } else if (name.compareTo("acr") == 0) { acr = (String) object; } else if (name.compareTo("amr") == 0) { amr = (String) object; } else if (name.compareTo("azp") == 0) { azp = (String) object; } else if (name.compareTo("tim_app_key") == 0) { tak = (String) object; } }
From source file:cz.muni.fi.japanesedictionary.entity.JapaneseCharacter.java
/** * Universal privatemethod for parsing JSON array. * //www. j a va 2s. c om * @param sense - JSONArray to be parsed */ private List<String> parseOneJSONArray(JSONArray sense) { if (sense == null) { return null; } List<String> temp = new ArrayList<>(); for (int k = 0; k < sense.length(); k++) { String value; try { value = sense.getString(k); temp.add(value); } catch (JSONException e) { Log.w(LOG_TAG, "getting parseOneSense() expression failed: " + e.toString()); e.printStackTrace(); } } return temp; }
From source file:com.extremeboredom.wordattack.utils.JSONUtils.java
/** * get String array from jsonObject//from ww w . ja v a 2 s . c o m * * @param jsonObject * @param key * @param defaultValue * @return <ul> * <li>if jsonObject is null, return defaultValue</li> * <li>if key is null or empty, return defaultValue</li> * <li>if {@link JSONObject#getJSONArray(String)} exception, return defaultValue</li> * <li>if {@link JSONArray#getString(int)} exception, return defaultValue</li> * <li>return string array</li> * </ul> */ public static String[] getStringArray(JSONObject jsonObject, String key, String[] defaultValue) { if (jsonObject == null || StringUtils.isEmpty(key)) { return defaultValue; } try { JSONArray statusArray = jsonObject.getJSONArray(key); if (statusArray != null) { String[] value = new String[statusArray.length()]; for (int i = 0; i < statusArray.length(); i++) { value[i] = statusArray.getString(i); } return value; } } catch (JSONException e) { if (isPrintException) { e.printStackTrace(); } return defaultValue; } return defaultValue; }
From source file:com.extremeboredom.wordattack.utils.JSONUtils.java
/** * get String list from jsonObject/* ww w.j a v a2 s .c om*/ * * @param jsonObject * @param key * @param defaultValue * @return <ul> * <li>if jsonObject is null, return defaultValue</li> * <li>if key is null or empty, return defaultValue</li> * <li>if {@link JSONObject#getJSONArray(String)} exception, return defaultValue</li> * <li>if {@link JSONArray#getString(int)} exception, return defaultValue</li> * <li>return string array</li> * </ul> */ public static List<String> getStringList(JSONObject jsonObject, String key, List<String> defaultValue) { if (jsonObject == null || StringUtils.isEmpty(key)) { return defaultValue; } try { JSONArray statusArray = jsonObject.getJSONArray(key); if (statusArray != null) { List<String> list = new ArrayList<String>(); for (int i = 0; i < statusArray.length(); i++) { list.add(statusArray.getString(i)); } return list; } } catch (JSONException e) { if (isPrintException) { e.printStackTrace(); } return defaultValue; } return defaultValue; }
From source file:com.example.android.popularmoviesist2.data.FetchTrailerMovieTask.java
private String[] getMoviesDataFromJson(String moviesJsonStr) throws JSONException { final String JSON_LIST = "results"; final String JSON_ID = "id"; final String JSON_KEY = "key"; final String JSON_NAME = "name"; final String JSON_SITE = "site"; urlYoutbe.clear();/* ww w . j av a2 s . co m*/ try { JSONObject moviesJson = new JSONObject(moviesJsonStr); JSONArray moviesArray = moviesJson.getJSONArray(JSON_LIST); final String[] result = new String[moviesArray.length()]; for (int i = 0; i < moviesArray.length(); i++) { JSONObject movie = moviesArray.getJSONObject(i); String id = movie.getString(JSON_ID); String key = movie.getString(JSON_KEY); String name = movie.getString(JSON_NAME); String site = movie.getString(JSON_SITE); result[i] = key; } return result; } catch (JSONException e) { Log.e(LOG_TAG, e.getMessage(), e); e.printStackTrace(); return null; } }