Example usage for org.json JSONArray JSONArray

List of usage examples for org.json JSONArray JSONArray

Introduction

In this page you can find the example usage for org.json JSONArray JSONArray.

Prototype

public JSONArray(Object array) throws JSONException 

Source Link

Document

Construct a JSONArray from an array

Usage

From source file:com.tweetlanes.android.core.App.java

public void removeAccount(String accountKey) {
    final Editor edit = mPreferences.edit();
    String accountIndices = mPreferences.getString(SharedPreferencesConstants.ACCOUNT_INDICES, null);
    if (accountIndices != null) {
        try {/*from w w w  .ja  v a2 s .c o  m*/
            JSONArray jsonArray = new JSONArray(accountIndices);
            JSONArray newIndicies = new JSONArray();
            for (int i = 0; i < jsonArray.length(); i++) {
                Long id = jsonArray.getLong(i);

                String key = getAccountDescriptorKey(id);
                String jsonAsString = mPreferences.getString(key, null);
                if (jsonAsString != null) {
                    AccountDescriptor account = new AccountDescriptor(this, jsonAsString);

                    if (!account.getAccountKey().equals(accountKey)) {
                        newIndicies.put(Long.toString(id));
                    } else {
                        ArrayList<LaneDescriptor> lanes = account.getAllLaneDefinitions();
                        for (LaneDescriptor lane : lanes) {
                            String lanekey = lane.getCacheKey(account.getScreenName() + account.getId());
                            edit.remove(lanekey);
                        }

                        edit.remove(key);
                        mAccounts.remove(account);
                    }
                }
            }

            accountIndices = newIndicies.toString();
            edit.putString(SharedPreferencesConstants.ACCOUNT_INDICES, accountIndices);

            edit.commit();

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    updateTwitterAccountCount();
}

From source file:com.tweetlanes.android.core.App.java

void updateTwitterAccountCount() {

    mAccounts.clear();/*from  ww w  .  j  a  v a  2 s  . c  o  m*/

    long currentAccountId = mPreferences.getLong(SharedPreferencesConstants.CURRENT_ACCOUNT_ID, -1);
    String accountIndices = mPreferences.getString(SharedPreferencesConstants.ACCOUNT_INDICES, null);
    if (accountIndices != null) {
        try {
            JSONArray jsonArray = new JSONArray(accountIndices);
            for (int i = 0; i < jsonArray.length(); i++) {
                Long id = jsonArray.getLong(i);

                String key = getAccountDescriptorKey(id);
                String jsonAsString = mPreferences.getString(key, null);
                if (jsonAsString != null) {
                    AccountDescriptor account = new AccountDescriptor(this, jsonAsString);
                    if (Constant.ENABLE_APP_DOT_NET == false
                            && account.getSocialNetType() == SocialNetConstant.Type.Appdotnet) {
                        continue;
                    }
                    mAccounts.add(account);

                    if (currentAccountId != -1 && account.getId() == currentAccountId) {
                        mCurrentAccountIndex = i;
                    }
                }
            }

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    if (mCurrentAccountIndex == null && mAccounts.size() > 0) {
        mCurrentAccountIndex = 0;
    }
}

From source file:com.poguico.palmabici.parsers.Parser.java

public static ArrayList<Station> parseNetworkJSON(String data) {
    ArrayList<Station> stations = new ArrayList<Station>();
    JSONArray jsonArray;//  ww  w  .  jav  a2s  . c om
    JSONObject jsonObject;
    Long lngAcum = 0L, latAcum = 0L;
    int id;

    try {
        jsonArray = new JSONArray(data);

        for (int i = 0; i < jsonArray.length(); i++) {
            jsonObject = jsonArray.getJSONObject(i);

            lngAcum += jsonObject.getLong("lng");
            latAcum += jsonObject.getLong("lat");

            id = jsonObject.getInt("id");
            stations.add(new Station(id, jsonObject.getString("name"), jsonObject.getDouble("lng") / 1e6,
                    jsonObject.getDouble("lat") / 1e6, jsonObject.getInt("free"), jsonObject.getInt("bikes"),
                    false, NetworkStationAlarm.hasAlarm(id)));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return stations;
}

From source file:org.wso2.carbon.ml.integration.common.utils.MLHttpClient.java

/**
 * Get a ID of the first version-set of a dataset
 * //www . j a  v a2  s .  co  m
 * @param datasetId ID of the dataset
 * @return          ID of the first versionset of the dataset
 * @throws          ClientProtocolException
 * @throws          MLHttpClientException 
 */
public int getAVersionSetIdOfDataset(int datasetId) throws MLHttpClientException {
    CloseableHttpResponse response;
    try {
        response = doHttpGet("/api/datasets/" + datasetId + "/versions");
        // Get the Id of the first dataset
        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(response.getEntity().getContent()));
        JSONArray responseJson = new JSONArray(bufferedReader.readLine());
        JSONObject datsetVersionJson = (JSONObject) responseJson.get(0);
        return datsetVersionJson.getInt("id");
    } catch (Exception e) {
        throw new MLHttpClientException("Failed to get a version set ID of dataset: " + datasetId, e);
    }
}

From source file:org.wso2.carbon.ml.integration.common.utils.MLHttpClient.java

/**
 * @param response {@link CloseableHttpResponse}
 * @return null if response is invalid. Json as string, if it is a valid response.
 * @throws MLHttpClientException/* w w w. j  a v a2  s . c o  m*/
 */
public String getResponseAsString(CloseableHttpResponse response) throws MLHttpClientException {
    if (response == null || response.getEntity() == null) {
        return null;
    }
    String reply = null;
    try {
        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(response.getEntity().getContent()));
        String line = bufferedReader.readLine();
        try {
            JSONObject responseJson = new JSONObject(line);
            reply = responseJson.toString();
        } catch (JSONException e) {
            JSONArray responseArray = new JSONArray(line);
            reply = responseArray.toString();
        }
        bufferedReader.close();
        response.close();
        return reply;
    } catch (Exception e) {
        throw new MLHttpClientException("Failed to extract the response body.", e);
    }
}

From source file:nl.hnogames.domoticzapi.Parsers.WeatherParser.java

@Override
public void parseResult(String result) {

    try {//  ww w  . j  a v a2s  .com
        JSONArray jsonArray = new JSONArray(result);
        ArrayList<WeatherInfo> mWeathers = new ArrayList<>();

        if (jsonArray.length() > 0) {

            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject row = jsonArray.getJSONObject(i);
                mWeathers.add(new WeatherInfo(row));
            }
        }

        WeatherReceiver.onReceiveWeather(mWeathers);

    } catch (JSONException e) {
        Log.e(TAG, "WeatherParser JSON exception");
        e.printStackTrace();
        WeatherReceiver.onError(e);
    }
}

From source file:com.project.merauke.CustomItemizedOverlay.java

/**
 * extract JSON into usable data//  www .j  ava 2 s  .com
 * */
private ArrayList<CustomOverlayItem> processingJSON(String strJSON) {
    ArrayList<CustomOverlayItem> listPoints = new ArrayList<CustomOverlayItem>();
    try {
        JSONArray rowArray = new JSONArray(strJSON);
        // Log.d("JSON", "total: "+rowArray.length());

        int count = 0;
        JSONObject jsonElement = null;
        int posLat = 0;
        int posLng = 0;
        String name = "";
        String alamat = "";

        if (rowArray.length() != 0) {
            this.removeAll();// TODO robust impl.

            while (count < rowArray.length()) {

                jsonElement = rowArray.getJSONObject(count);
                posLat = (int) (Double.parseDouble(jsonElement.getString("lat")) * 1E6);
                posLng = (int) (Double.parseDouble(jsonElement.getString("lng")) * 1E6);
                name = jsonElement.getString("namalokasi");
                alamat = jsonElement.getString("alamat");

                listPoints.add(new CustomOverlayItem(new GeoPoint(posLat, posLng), name, alamat,
                        "http://ia.media-imdb.com/images/M/MV5BMTM1MTk2ODQxNV5BMl5BanBnXkFtZTcwOTY5MDg0NA@@._V1._SX40_CR0,0,40,54_.jpg"));

                count++;
            }
            Log.d("JSON", "loading data finish");
        } else {
            Log.d("JSON", "EMPTY");
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return listPoints;
}

From source file:de.damdi.fitness.db.parser.ExerciseTagJSONParser.java

/**
 * Parses the JSON-String to a list of {@link ExerciseTag}s.
 * /*from   ww w  . j a v  a  2s. co  m*/
  *   
  * @param jsonString The String to parse.
  * 
  * @return A list of {@link ExerciseTag}s, null if an error occurs.
  * 
 */
@Override
public List<ExerciseTag> parse(String jsonString) {
    List<ExerciseTag> exerciseTagList = new ArrayList<ExerciseTag>();

    JSONArray jsonArray;
    try {
        jsonArray = new JSONArray(jsonString);

        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject SportsEquipmentObject = jsonArray.getJSONObject(i);

            ExerciseTag exerciseTag = null;

            for (String locale : TAG_LOCALES) {
                if (SportsEquipmentObject.has(locale)) {
                    JSONObject languageObject = SportsEquipmentObject.getJSONObject(locale);

                    // name
                    String name = languageObject.getString(TAG_NAME);
                    List<String> nameList = new ArrayList<String>();
                    nameList.add(name);

                    String description = null;

                    // description   
                    if (languageObject.has(TAG_DESCRIPTION)) {
                        //JSONObject descriptionJSONObject = languageObject.getJSONObject(TAG_DESCRIPTION);

                        // description
                        description = languageObject.getString(TAG_DESCRIPTION);

                    }

                    if (exerciseTag == null) {
                        exerciseTag = new ExerciseTag(new Locale(locale), nameList, description);
                    } else {
                        exerciseTag.addNames(new Locale(locale), nameList, description);
                    }
                }
            }

            // Log.d(TAG, "Finished parsing ExerciseTag: \n" + exerciseTag.toDebugString());
            exerciseTagList.add(exerciseTag);

        }

    } catch (JSONException e) {
        Log.e(TAG, "Error during parsing JSON File.", e);
        return null;
    }

    if (exerciseTagList.isEmpty())
        throw new AssertionError("JSON parsing failed: no ExerciseTag parsed.");
    return exerciseTagList;
}

From source file:br.unicamp.cst.behavior.bn.Behavior.java

/**
 * @return the actionsSet//from ww  w  .  j a  v a 2s  .c  om
 */
public String getActionList()//TODO a simpler version of this might be interesting
{
    //      return actionList;
    try {
        JSONArray teste = new JSONArray(jsonActionList.toString());

    } catch (JSONException e) {

        e.printStackTrace();
    }

    return jsonActionList.toString();
}

From source file:com.mobeelizer.java.connection.MobeelizerConnectionServiceImpl.java

@Override
public MobeelizerOperationStatus<List<String>> getGroups() {
    try {/*  w  w  w.ja v a2s.  co m*/
        MobeelizerOperationStatus<String> response = executeGetAndGetContent("/client/user/groups");
        if (response.getError() != null) {
            return new MobeelizerOperationStatus<List<String>>(response.getError());
        }
        JSONArray json = new JSONArray(response.getContent());

        List<String> groups = new ArrayList<String>();

        for (int i = 0; i < json.length(); i++) {
            groups.add(json.getJSONObject(i).getString("name"));
        }
        return new MobeelizerOperationStatus<List<String>>(groups);
    } catch (JSONException e) {
        return new MobeelizerOperationStatus<List<String>>(MobeelizerOperationErrorImpl.exception(e));
    }
}