public static TeamsRank parseTeamsRank(String jsonStr) { TeamsRank rank = new TeamsRank(); String dataStr = JsonParser.parseBase(rank, jsonStr); rank.east = new ArrayList<>(); rank.west = new ArrayList<>(); try { JSONArray jsonArray = new JSONArray(dataStr); for (int i = 0; i < jsonArray.length(); i++) { org.json.JSONObject item = jsonArray.getJSONObject(i); // 得到每个对象 String title = item.getString("title"); JSONArray teamsArray = item.optJSONArray("rows"); for (int j = 0; j < teamsArray.length(); j++) { JSONArray teamsInfo = teamsArray.getJSONArray(j); Gson gson = new Gson(); TeamsRank.TeamBean bean = gson.fromJson(teamsInfo.getString(0), TeamsRank.TeamBean.class); bean.win = teamsInfo.optInt(1); bean.lose = teamsInfo.optInt(2); bean.rate = teamsInfo.optString(3); bean.difference = teamsInfo.optString(4); if (title.equals("东部联盟")) { rank.east.add(bean); } else { rank.west.add(bean); } } } return rank; } catch (Exception e) { LogUtils.e(e.toString()); } return null; }
private Animation readAnimation(JSONObject root, String animKind, TextureFilm film) { try { JSONObject jsonAnim = root.getJSONObject(animKind); Animation anim = new Animation(jsonAnim.getInt("fps"), jsonAnim.getBoolean("looped")); List<Integer> framesSeq = new ArrayList<Integer>(16); JSONArray jsonFrames = jsonAnim.getJSONArray("frames"); int nextFrame; for (int i = 0; (nextFrame = jsonFrames.optInt(i, -1)) != -1; ++i) { framesSeq.add(nextFrame); } anim.frames(film, framesSeq, kind * framesInRow); return anim; } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }
private static int[] parseVersionSpec(JSONArray versionsJSON) { // Null signifies no overrides to the min-version as specified by the SDK. // An empty array would basically turn off the dialog (i.e no supported versions), so // DON'T default to that. int[] versionSpec = null; if (versionsJSON != null) { int numVersions = versionsJSON.length(); versionSpec = new int[numVersions]; for (int i = 0; i < numVersions; i++) { // See if the version was stored directly as an Integer int version = versionsJSON.optInt(i, NativeProtocol.NO_PROTOCOL_AVAILABLE); if (version == NativeProtocol.NO_PROTOCOL_AVAILABLE) { // If not, then see if it was stored as a string that can be parsed out. // If even that fails, then we will leave it as NO_PROTOCOL_AVAILABLE String versionString = versionsJSON.optString(i); if (!isNullOrEmpty(versionString)) { try { version = Integer.parseInt(versionString); } catch (NumberFormatException nfe) { logd(LOG_TAG, nfe); version = NativeProtocol.NO_PROTOCOL_AVAILABLE; } } } versionSpec[i] = version; } } return versionSpec; }
/** * Parse int array from JSONObject with given name. * * @param from int JSON array like this one {@code {11, 34, 42}} */ public static int[] parseIntArray(JSONArray from) { int[] result = new int[from.length()]; for (int i = 0; i < result.length; i++) { result[i] = from.optInt(i); } return result; }
/** method to update data in caching layer object from JSON * */ public boolean updateFromJSON(JSONObject jsonObj) { try { JSONArray hasArcsArray = jsonObj.optJSONArray("hasArcs"); if (hasArcsArray != null) { ArrayList<Arc> aListOfHasArcs = new ArrayList<Arc>(hasArcsArray.length()); for (int i = 0; i < hasArcsArray.length(); i++) { int id = hasArcsArray.optInt(i); if (ArcManager.getInstance().get(id) != null) { aListOfHasArcs.add(ArcManager.getInstance().get(id)); } } setHasArcs(aListOfHasArcs); } if (!jsonObj.isNull("hasPropertySet")) { int hasPropertySetId = jsonObj.optInt("hasPropertySet"); PropertySet value = PropertySetManager.getInstance().get(hasPropertySetId); if (value != null) { setHasPropertySet(value); } } if (!jsonObj.isNull("hasDomainNodes")) { int hasDomainNodesId = jsonObj.optInt("hasDomainNodes"); NodeList value = NodeListManager.getInstance().get(hasDomainNodesId); if (value != null) { setHasDomainNodes(value); } } } catch (Exception e) { logWriter.error("Failure updating from JSON", e); return false; } return true; }
/** * This method parses the child ages elements and returns a list. * * @param jsonChildAges JSON object to parse child ages from. * @return Parsed list of child ages. */ private List<Integer> parseChildAgeArray(final JSONArray jsonChildAges) { if (jsonChildAges != null) { final List<Integer> childAges = new ArrayList<Integer>(jsonChildAges.length()); for (int i = 0; i < jsonChildAges.length(); i++) { childAges.add(jsonChildAges.optInt(i)); } return childAges; } return null; }
@SmallTest @MediumTest @LargeTest public void testCollectionAdd() throws JSONException { JSONArray array = new JSONArray(); Collection<Integer> collection = GraphObject.Factory.createList(array, Integer.class); collection.add(5); assertTrue(array.length() == 1); assertTrue(array.optInt(0) == 5); }
private Map<Integer, List<JSONArray>> buildErrorMap(JSONArray errors) { Map<Integer, List<JSONArray>> result = new HashMap<Integer, List<JSONArray>>(); if (errors != null) { for (int i = 0; i < errors.length(); i++) { JSONArray error = errors.optJSONArray(i); if (error != null && error.length() > 0) { int index = error.optInt(0); List<JSONArray> errorList = result.get(index); if (errorList == null) { errorList = new LinkedList<JSONArray>(); result.put(index, errorList); } errorList.add(error); } } } return result; }
private Animation readAnimation(JSONObject root, String animKind, TextureFilm film) throws JSONException { JSONObject jsonAnim = root.getJSONObject(animKind); Animation anim = new Animation(jsonAnim.getInt("fps"), jsonAnim.getBoolean("looped")); List<Integer> framesSeq = new ArrayList<Integer>(16); JSONArray jsonFrames = jsonAnim.getJSONArray("frames"); int nextFrame; for (int i = 0; (nextFrame = jsonFrames.optInt(i, -1)) != -1; ++i) { framesSeq.add(nextFrame); } anim.frames(film, framesSeq, kind * framesInRow); return anim; }
public ArrayList<ArrayList<Metric>> parseDiscoverPage(JSONArray discovered_page) { ArrayList<Metric> eachMovie = new ArrayList<Metric>(); ArrayList<ArrayList<Metric>> allResults = new ArrayList<ArrayList<Metric>>(); JSONArray obj = discovered_page; // System.out.println("DISC PAGE: " + discovered_page); if (resultsLeft > 20) { for (int i = 0; i <= 19; i++) { // "overview" String overview = obj.optJSONObject(i).optString("overview"); Overview overviewToAdd = new Overview(overview); eachMovie.add(overviewToAdd); // "original_lanuage" String original_language = obj.optJSONObject(i).optString("original_language"); Original_Language originalLanguageToAdd = new Original_Language(original_language); eachMovie.add(originalLanguageToAdd); // "original_title" String original_title = obj.optJSONObject(i).optString("original_title"); Original_Title originalTitleToAdd = new Original_Title(original_title); eachMovie.add(originalTitleToAdd); // "video" boolean video = obj.optJSONObject(i).optBoolean("video"); Video vidToAdd = new Video(video); eachMovie.add(vidToAdd); // "title" String title = obj.optJSONObject(i).optString("title"); Movie movieToAdd = new Movie(title, 0, 0); // "genre IDs" JSONArray genre_ids = obj.optJSONObject(i).optJSONArray("genre_ids"); // "genres" - Array Elements int genre_id = 0; // "genres" - Extraction if (genre_ids != null) { for (int j = 0; j < genre_ids.length(); j++) { genre_id = genre_ids.optInt(j); Genre genreToAdd = new Genre("", genre_id); eachMovie.add(genreToAdd); // System.out.println(genre_id); } } // "poster_path" String poster_path = obj.optJSONObject(i).optString("poster_path"); Poster_Path pPathToAdd = new Poster_Path(poster_path); eachMovie.add(pPathToAdd); // "backdrop_path" String backdrop_path = obj.optJSONObject(i).optString("backdrop_path"); Backdrop_Path bPathToAdd = new Backdrop_Path(backdrop_path); eachMovie.add(bPathToAdd); // "release_date" String release_date = obj.optJSONObject(i).optString("release_date"); Release_Date rDateToAdd = new Release_Date(release_date); eachMovie.add(rDateToAdd); // "popularity" double popularity = obj.optJSONObject(i).optDouble("popularity"); Popularity popToAdd = new Popularity(popularity); eachMovie.add(popToAdd); // "vote_average" double vote_average = obj.optJSONObject(i).optDouble("vote_average"); Vote_Average vAverageToAdd = new Vote_Average(vote_average); eachMovie.add(vAverageToAdd); // "id" int id = obj.optJSONObject(i).optInt("id"); movieToAdd.setMovieID(id); eachMovie.add(movieToAdd); // "adult" boolean adult = obj.optJSONObject(i).optBoolean("adult"); Adult adultToAdd = new Adult(adult); eachMovie.add(adultToAdd); // "vote_count" int vote_count = obj.optJSONObject(i).optInt("vote_count"); Vote_Count vCountToAdd = new Vote_Count(vote_count); eachMovie.add(vCountToAdd); // System.out.println(overview); // System.out.println(original_language); // System.out.println(original_title); // System.out.println(video); // System.out.println(poster_path); // System.out.println(title); // System.out.println(backdrop_path); // System.out.println(release_date); // System.out.println(popularity); // System.out.println(vote_average); // System.out.println(id); // System.out.println(adult); // System.out.println(vote_count); allResults.add(eachMovie); resultsLeft--; } } else { for (int i = 0; i < resultsLeft; i++) { // "overview" String overview = obj.optJSONObject(i).optString("overview"); Overview overviewToAdd = new Overview(overview); eachMovie.add(overviewToAdd); // "original_lanuage" String original_language = obj.optJSONObject(i).optString("original_language"); Original_Language originalLanguageToAdd = new Original_Language(original_language); eachMovie.add(originalLanguageToAdd); // "original_title" String original_title = obj.optJSONObject(i).optString("original_title"); Original_Title originalTitleToAdd = new Original_Title(original_title); eachMovie.add(originalTitleToAdd); // "video" boolean video = obj.optJSONObject(i).optBoolean("video"); Video vidToAdd = new Video(video); eachMovie.add(vidToAdd); // "title" String title = obj.optJSONObject(i).optString("title"); // "genre IDs" JSONArray genre_ids = obj.optJSONObject(i).optJSONArray("genre_ids"); // "genres" - Array Elements int genre_id = 0; // "genres" - Extraction if (genre_ids != null) { for (int j = 0; j < genre_ids.length(); j++) { genre_id = genre_ids.optInt(j); // System.out.println(genre_id); } } // "poster_path" String poster_path = obj.optJSONObject(i).optString("poster_path"); Poster_Path pPathToAdd = new Poster_Path(poster_path); eachMovie.add(pPathToAdd); // "backdrop_path" String backdrop_path = obj.optJSONObject(i).optString("backdrop_path"); Backdrop_Path bPathToAdd = new Backdrop_Path(backdrop_path); eachMovie.add(bPathToAdd); // "release_date" String release_date = obj.optJSONObject(i).optString("release_date"); Release_Date rDateToAdd = new Release_Date(release_date); eachMovie.add(rDateToAdd); // "popularity" double popularity = obj.optJSONObject(i).optDouble("popularity"); Popularity popToAdd = new Popularity(popularity); eachMovie.add(popToAdd); // "vote_average" double vote_average = obj.optJSONObject(i).optDouble("vote_average"); Vote_Average vAverageToAdd = new Vote_Average(vote_average); eachMovie.add(vAverageToAdd); // "id" int id = obj.optJSONObject(i).optInt("id"); Movie movieToAdd = new Movie(title, id, 0); eachMovie.add(movieToAdd); // "adult" boolean adult = obj.optJSONObject(i).optBoolean("adult"); Adult adultToAdd = new Adult(adult); eachMovie.add(adultToAdd); // "vote_count" int vote_count = obj.optJSONObject(i).optInt("vote_count"); Vote_Count vCountToAdd = new Vote_Count(vote_count); eachMovie.add(vCountToAdd); // System.out.println(overview); // System.out.println(original_language); // System.out.println(original_title); // System.out.println(video); // System.out.println(poster_path); // System.out.println(title); // System.out.println(backdrop_path); // System.out.println(release_date); // System.out.println(popularity); // System.out.println(vote_average); // System.out.println(id); // System.out.println(adult); // System.out.println(vote_count); allResults.add(eachMovie); } } return allResults; }
public int optInt(int index) { return baseArgs.optInt(index); }
/** * Parses the supplied JSON string and builds a list of apps from it * * @param json * @param accountName * @return List of apps * @throws JSONException */ static List<AppInfo> parseAppInfos(String json, String accountName) throws JSONException { Date now = new Date(); List<AppInfo> apps = new ArrayList<AppInfo>(); // Extract the base array containing apps JSONArray jsonApps = new JSONObject(json).getJSONArray("result").getJSONArray(1); if (DEBUG) { pp("jsonApps", jsonApps); } int numberOfApps = jsonApps.length(); for (int i = 0; i < numberOfApps; i++) { AppInfo app = new AppInfo(); app.setAccount(accountName); app.setLastUpdate(now); /* * Per app: * null * [ APP_INFO_ARRAY * * null * * packageName * * Nested array with details * * null * * Nested array with version details * * Nested array with price details * * Last update Date * * Number [1=published, 5 = draft?] * ] * null * [ APP_STATS_ARRAY * * null, * * Active installs * * Total ratings * * Average rating * * Errors * * Total installs * ] */ JSONArray jsonApp = jsonApps.getJSONArray(i); JSONArray jsonAppInfo = jsonApp.getJSONArray(1); if (DEBUG) { pp("jsonAppInfo", jsonAppInfo); } String packageName = jsonAppInfo.getString(1); // Look for "tmp.7238057230750432756094760456.235728507238057230542" if (packageName == null || (packageName.startsWith("tmp.") && Character.isDigit(packageName.charAt(4)))) { continue; // Draft app } // Check number code and last updated date // Published: 1 // Unpublished: 2 // Draft: 5 // Draft w/ in-app items?: 6 // TODO figure out the rest and add don't just skip, filter, etc. Cf. #223 int publishState = jsonAppInfo.getInt(7); Log.d(TAG, String.format("%s: publishState=%d", packageName, publishState)); if (publishState != 1) { // Not a published app, skipping continue; } app.setPublishState(publishState); app.setPackageName(packageName); /* * Per app details: * null * Country code * App Name * Description * Unknown * Last what's new */ if (jsonAppInfo.length() < 5) { // skip if we can't get all the data continue; } JSONArray appDetails = jsonAppInfo.getJSONArray(2).getJSONArray(1).getJSONArray(0); if (DEBUG) { pp("appDetails", appDetails); } app.setName(appDetails.getString(2)); /* * Per app version details: * null * null * packageName * versionNumber * versionName * null * Array with app icon [null,null,null,icon] */ JSONArray appVersions = jsonAppInfo.optJSONArray(4); if (DEBUG) { pp("appVersions", appVersions); } if (appVersions == null) { continue; } JSONArray lastAppVersionDetails = appVersions.getJSONArray(appVersions.length() - 1).getJSONArray(2); if (DEBUG) { pp("lastAppVersionDetails", lastAppVersionDetails); } app.setVersionName(lastAppVersionDetails.getString(4)); app.setIconUrl(lastAppVersionDetails.getJSONArray(6).getString(3)); // App stats /* * null, * Active installs * Total ratings * Average rating * Errors * Total installs */ // XXX this index might not be correct for all apps? JSONArray jsonAppStats = jsonApp.optJSONArray(3); if (DEBUG) { pp("jsonAppStats", jsonAppStats); } if (jsonAppStats == null) { continue; } AppStats stats = new AppStats(); stats.setRequestDate(now); if (jsonAppStats.length() < 6) { // no statistics (yet?) or weird format // TODO do we need differentiate? stats.setActiveInstalls(0); stats.setTotalDownloads(0); stats.setNumberOfErrors(0); } else { stats.setActiveInstalls(jsonAppStats.getInt(1)); stats.setTotalDownloads(jsonAppStats.getInt(5)); stats.setNumberOfErrors(jsonAppStats.optInt(4)); } app.setLatestStats(stats); apps.add(app); } return apps; }