List of usage examples for org.json JSONArray put
public JSONArray put(int index, Object value) throws JSONException
From source file:com.maxleapmobile.gitmaster.ui.fragment.RecommendFragment.java
private void compareGenes() { Logger.d("compareGenes start"); MLQueryManager.findAllInBackground(query, new FindCallback<MLObject>() { @Override/*from w w w. j a v a2s . c o m*/ public void done(List<MLObject> list, MLException e) { if (e == null) { Logger.d("compareGenes success"); boolean needRefresh = false; ArrayList<Gene> newGenes = new ArrayList<>(); for (int i = 0; i < list.size(); i++) { Gene gene = Gene.from(list.get(i)); if (!needRefresh && !genes.contains(gene)) { genes.add(gene); needRefresh = true; } newGenes.add(gene); } if (needRefresh) { genes = newGenes; JSONArray jsonArray = new JSONArray(); for (int i = 0; i < genes.size(); i++) { JSONObject jsonObject = new JSONObject(); try { jsonObject.put("language", genes.get(i).getLanguage()); jsonObject.put("skill", genes.get(i).getSkill()); jsonArray.put(i, jsonObject); } catch (Exception jsonException) { } } mParmasMap.put("genes", jsonArray); isEnd = false; isReview = false; repos.clear(); fetchTrendingGeneDate(); } else { loadUrl(); mProgressBar.setVisibility(View.GONE); mEmptyView.setVisibility(View.GONE); actionArea.setEnabled(true); } } else { loadUrl(); Logger.d("compareGenes failed"); mProgressBar.setVisibility(View.GONE); } } }); }
From source file:edu.stanford.mobisocial.dungbeetle.feed.objects.InviteToSharedAppFeedObj.java
public static JSONObject json(Collection<Contact> contacts, String feedName, String packageName) { JSONObject obj = new JSONObject(); try {/*from w w w .ja va 2 s. com*/ obj.put("packageName", packageName); obj.put("sharedFeedName", feedName); JSONArray participants = new JSONArray(); Iterator<Contact> it = contacts.iterator(); while (it.hasNext()) { String localId = "@l" + it.next().id; participants.put(participants.length(), localId); } // Need to add ourself to participants participants.put(participants.length(), "@l" + Contact.MY_ID); obj.put("participants", participants); } catch (JSONException e) { } return obj; }
From source file:com.seamusdawkins.rest.utils.HttpHelper.java
public static JSONArray doGetArray(Activity act, String url, String charset, String token) throws IOException, Exception, JSONException { HttpGet conn = new HttpGet(url); conn.addHeader(X_APPLICATION_TOKEN, token); conn.addHeader(CONNECTION, "close"); conn.addHeader(CONTENT_TYPE, "application/x-www-form-urlencoded"); JSONArray o = null;//from www.j a v a 2 s . c o m StringBuilder sb = new StringBuilder(); resp = null; try { HttpClient httpClient = new DefaultHttpClient(); resp = httpClient.execute(conn); } catch (ClientProtocolException e) { } catch (IOException e) { } try { if (resp != null) { status = resp.getStatusLine().getStatusCode(); if (status == 200) { InputStream content = resp.getEntity().getContent(); BufferedReader buffer = new BufferedReader(new InputStreamReader(content)); String text; while ((text = buffer.readLine()) != null) { sb.append(text); } o = new JSONArray(sb.toString()); } else { JSONArray arr = new JSONArray(); JSONObject obj = new JSONObject(); obj.put("statuscode", String.valueOf(status)); arr.put(0, obj); return arr; } } } catch (Exception e) { return null; } return o; }
From source file:es.upm.dit.xsdinferencer.extraction.extractorImpl.JSONTypesExtractorImpl.java
/** * This method adds additional quotes to any String value inside a JSON. * This helps to distinguish whether values come from strings or other * primitive types when the JSON is converted to XML. * @param jsonRoot/*from w w w . j ava 2 s . c om*/ */ private void quoteStringsAtJSON(Object jsonRoot) { if (jsonRoot instanceof JSONObject) { JSONObject jsonObject = (JSONObject) jsonRoot; ImmutableSet<String> jsonObjectKeys = ImmutableSet.copyOf(jsonObject.keys()); for (String key : jsonObjectKeys) { Object value = jsonObject.get(key); if (value instanceof String) { String valueStr = (String) value; jsonObject.put(key, "\"" + valueStr + "\""); } else if (value instanceof JSONObject || value instanceof JSONArray) { quoteStringsAtJSON(value); } } } else if (jsonRoot instanceof JSONArray) { JSONArray jsonArray = (JSONArray) jsonRoot; for (int i = 0; i < jsonArray.length(); i++) { Object value = jsonArray.get(i); if (value instanceof String) { String valueStr = (String) value; jsonArray.put(i, "\"" + valueStr + "\""); } else if (value instanceof JSONObject || value instanceof JSONArray) { quoteStringsAtJSON(value); } } } else { return; } }
From source file:es.upm.dit.xsdinferencer.extraction.extractorImpl.JSONTypesExtractorImpl.java
/** * This method looks recursively for arrays and encapsulates each of them into another array. * So, anything like this:<br/>//w ww . ja v a 2s. c o m * <code> * {"myKey":["stuff1","stuff2"]} * </code><br/> * would become this:<br/> * <code> * {"myKey":[["stuff1","stuff2"]]} * </code><br/> * We do this strange preprocessing because structures like the first example are * converted to XML producing this result:<br/> * <code> * <myKey>stuff1</myKey><myKey>stuff2</myKey> * </code><br/> * Which makes impossible to distinguish single-element arrays from an element * outside any array, because, both this: * <code> * {"singleElement":["stuff"]} * </code><br/> * and this:<br/> * <code> * {"singleElement":"stuff"} * </code><br/> * Would be converted to:<br/> * <code> * <singleElement>stuff</singleElement> * </code><br/> * By doing this, we allow distingushing a single-element array from an non-array element, because * the first one would be converted to:<br/> * <code> * <singleElement><array>stuff</array></singleElement> * * @param jsonRoot The {@link JSONObject} or {@link JSONArray} which is the root of our JSON document. */ private void encapsulateArraysAtJSONRecursive(Object jsonRoot) { if (jsonRoot instanceof JSONObject) { JSONObject jsonObject = (JSONObject) jsonRoot; Set<String> keys = ImmutableSet.copyOf(jsonObject.keySet()); for (String key : keys) { Object value = jsonObject.get(key); encapsulateArraysAtJSONRecursive(value); if (value instanceof JSONArray) { JSONArray encapsulatingJsonArray = new JSONArray(); encapsulatingJsonArray.put(0, value); jsonObject.put(key, encapsulatingJsonArray); } } } else if (jsonRoot instanceof JSONArray) { JSONArray jsonArray = (JSONArray) jsonRoot; for (int i = 0; i < jsonArray.length(); i++) { Object value = jsonArray.get(i); encapsulateArraysAtJSONRecursive(value); if (value instanceof JSONArray) { JSONObject encapsulatingJsonObject = new JSONObject(); encapsulatingJsonObject.put(XSDInferenceConfiguration.ARRAY_ELEMENT_NAME, value); jsonArray.put(i, encapsulatingJsonObject); } } } else { return; } }
From source file:com.facebook.share.ShareApi.java
private void stageArrayList(final ArrayList arrayList, final CollectionMapper.OnMapValueCompleteListener onArrayListStagedListener) { final JSONArray stagedObject = new JSONArray(); final CollectionMapper.Collection<Integer> collection = new CollectionMapper.Collection<Integer>() { @Override/* ww w . ja v a 2s . c om*/ public Iterator<Integer> keyIterator() { final int size = arrayList.size(); final Mutable<Integer> current = new Mutable<Integer>(0); return new Iterator<Integer>() { @Override public boolean hasNext() { return current.value < size; } @Override public Integer next() { return current.value++; } @Override public void remove() { } }; } @Override public Object get(Integer key) { return arrayList.get(key); } @Override public void set(Integer key, Object value, CollectionMapper.OnErrorListener onErrorListener) { try { stagedObject.put(key, value); } catch (final JSONException ex) { String message = ex.getLocalizedMessage(); if (message == null) { message = "Error staging object."; } onErrorListener.onError(new FacebookException(message)); } } }; final CollectionMapper.OnMapperCompleteListener onStagedArrayMapperCompleteListener = new CollectionMapper.OnMapperCompleteListener() { @Override public void onComplete() { onArrayListStagedListener.onComplete(stagedObject); } @Override public void onError(FacebookException exception) { onArrayListStagedListener.onError(exception); } }; stageCollectionValues(collection, onStagedArrayMapperCompleteListener); }
From source file:com.watabou.pixeldungeon.utils.Bundle.java
public void put(String key, int[] array) { try {//from w ww . j a va2 s . c o m JSONArray jsonArray = new JSONArray(); for (int i = 0; i < array.length; i++) { jsonArray.put(i, array[i]); } data.put(key, jsonArray); } catch (JSONException e) { } }
From source file:com.watabou.pixeldungeon.utils.Bundle.java
public void put(String key, boolean[] array) { try {//from w ww.j ava2 s. com JSONArray jsonArray = new JSONArray(); for (int i = 0; i < array.length; i++) { jsonArray.put(i, array[i]); } data.put(key, jsonArray); } catch (JSONException e) { } }
From source file:com.watabou.pixeldungeon.utils.Bundle.java
public void put(String key, String[] array) { try {/*from w w w .j a v a 2s.c o m*/ JSONArray jsonArray = new JSONArray(); for (int i = 0; i < array.length; i++) { jsonArray.put(i, array[i]); } data.put(key, jsonArray); } catch (JSONException e) { } }
From source file:com.tweetlanes.android.App.java
public void onPostSignIn(TwitterUser user, String oAuthToken, String oAuthSecret, SocialNetConstant.Type oSocialNetType) { if (user != null) { try {//from w ww.j a v a 2s . co m final Editor edit = mPreferences.edit(); String userIdAsString = Long.toString(user.getId()); AccountDescriptor account = new AccountDescriptor(this, user, oAuthToken, oAuthSecret, oSocialNetType); edit.putString(getAccountDescriptorKey(user.getId()), account.toString()); String accountIndices = mPreferences.getString(SHARED_PREFERENCES_KEY_ACCOUNT_INDICES, null); JSONArray jsonArray; if (accountIndices == null) { jsonArray = new JSONArray(); jsonArray.put(0, user.getId()); mAccounts.add(account); } else { jsonArray = new JSONArray(accountIndices); boolean exists = false; for (int i = 0; i < jsonArray.length(); i++) { String c = jsonArray.getString(i); if (c.compareTo(userIdAsString) == 0) { exists = true; mAccounts.set(i, account); break; } } if (exists == false) { jsonArray.put(userIdAsString); mAccounts.add(account); } } accountIndices = jsonArray.toString(); edit.putString(SHARED_PREFERENCES_KEY_ACCOUNT_INDICES, accountIndices); edit.commit(); setCurrentAccount(user.getId()); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } updateTwitterAccountCount(); if (TwitterManager.get().getSocialNetType() == oSocialNetType) { TwitterManager.get().setOAuthTokenWithSecret(oAuthToken, oAuthSecret, true); } else { TwitterManager.initModule(oSocialNetType, oSocialNetType == SocialNetConstant.Type.Twitter ? ConsumerKeyConstants.TWITTER_CONSUMER_KEY : ConsumerKeyConstants.APPDOTNET_CONSUMER_KEY, oSocialNetType == SocialNetConstant.Type.Twitter ? ConsumerKeyConstants.TWITTER_CONSUMER_SECRET : ConsumerKeyConstants.APPDOTNET_CONSUMER_SECRET, oAuthToken, oAuthSecret, getCurrentAccountKey(), mConnectionStatusCallbacks); } } }