Example usage for org.json JSONArray getJSONObject

List of usage examples for org.json JSONArray getJSONObject

Introduction

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

Prototype

public JSONObject getJSONObject(int index) throws JSONException 

Source Link

Document

Get the JSONObject associated with an index.

Usage

From source file:com.jennifer.ui.chart.widget.LegendWidget.java

private JSONArray getLegendIcon(JSONObject brushObject) {
    JSONArray arr = new JSONArray();
    JSONArray data = JSONUtil.clone(brushObject.getJSONArray("target"));

    if (key != null && key.length() > 0) {
        data = chart.data();//from   w w w  .ja va  2  s  .  c  o m
    }

    int count = data.length();

    for (int i = 0; i < count; i++) {
        String text = "";

        if (key != null && key.length() > 0) {
            text = chart.series(key).optString("text", data.getJSONObject(i).getString(key));
        } else {
            String target = data.getString(i);
            text = chart.series(target).optString("text", target);
        }

        double rectWidth = (fontWidth - 4) * text.length();
        double width = Math.min(rectWidth, fontHeight);
        double height = width;

        Transform group = root.group(new JSONObject().put("class", "legend icon"));

        Transform rect = group.rect(new JSONObject().put("x", 0).put("y", 0).put("width", width)
                .put("height", height).put("fill", chart.color(i, brushObject.optJSONArray("colors"))));

        group.text(new JSONObject().put("x", width + 4).put("y", fontHeight - 3) // 3 is top, bottom font margin
                .put("font-family", chart.theme("fontFamily")).put("font-size", chart.theme("legendFontSize"))
                .put("fill", chart.theme("legendFontColor")).put("text-anchor", "start")).textNode(text);

        arr.put(new JSONObject().put("width", width + 4 + rectWidth + (i == count - 1 ? 0 : 10))
                .put("height", height + 4).put("icon", group)

        );

    }

    return arr;
}

From source file:com.chess.genesis.net.SyncClient.java

private void saveMsgs(final JSONObject data) {
    try {//from ww w. j  a  v  a 2  s. c  om
        final JSONArray msgs = data.getJSONArray("msglist");
        final long time = data.getLong("time");
        final GameDataDB db = new GameDataDB(context);

        for (int i = 0, len = msgs.length(); i < len; i++) {
            final JSONObject item = msgs.getJSONObject(i);
            db.insertMsg(item);
        }
        db.close();

        // Save sync time
        final PrefEdit pref = new PrefEdit(context);
        pref.putLong(R.array.pf_lastmsgsync, time);
        pref.commit();
    } catch (final JSONException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}

From source file:com.foxykeep.datadroidpoc.data.factory.PhoneDeleteFactory.java

public static long[] parseResult(String wsResponse) throws DataException {

    long[] deletedPhoneIdArray = null;

    try {//from w w  w  .  jav  a 2 s . c  o  m
        JSONObject parser = new JSONObject(wsResponse);
        JSONObject jsonRoot = parser.getJSONObject(JSONTag.CRUD_PHONE_DELETE_ELEM_PHONES);
        JSONArray jsonPhoneArray = jsonRoot.getJSONArray(JSONTag.CRUD_PHONE_DELETE_ELEM_PHONE);
        int size = jsonPhoneArray.length();

        deletedPhoneIdArray = new long[size];

        for (int i = 0; i < size; i++) {
            deletedPhoneIdArray[i] = jsonPhoneArray.getJSONObject(i).getLong(JSONTag.CRUD_PHONE_DELETE_ELEM_ID);
        }
    } catch (JSONException e) {
        Log.e(TAG, "JSONException", e);
        throw new DataException(e);
    }

    return deletedPhoneIdArray;
}

From source file:com.lmpessoa.sonarview.core.Server.java

public List<Issue> getIssues(String projectId) throws MalformedURLException, IOException {
    int pageIndex = 1;
    int total = Integer.MAX_VALUE;
    List<Issue> result = new ArrayList<>();
    while (result.size() < total) {
        JSONObject response = readIssuesFromServer(projectId, pageIndex);
        if (total == Integer.MAX_VALUE) {
            total = response.getInt("total");
        }/*  w  w  w .j  a v  a  2s .com*/
        JSONArray issues = response.getJSONArray("issues");
        for (int i = 0; i < issues.length(); ++i) {
            result.add(new Issue(issues.getJSONObject(i)));
        }
        pageIndex += 1;
    }
    return result;
}

From source file:com.rapid.actions.Logic.java

public Logic(RapidHttpServlet rapidServlet, JSONObject jsonAction) throws Exception {
    // call the super parameterless constructor which sets the xml version
    super();/*from w  ww  .j av a  2s  .c o m*/
    // save all key/values from the json into the properties 
    for (String key : JSONObject.getNames(jsonAction)) {
        // add all json properties to our properties, except for the ones we want directly accessible
        if (!"conditions".equals(key) && !"conditionsType".equals(key) && !"trueActions".equals(key)
                && !"falseActions".equals(key))
            addProperty(key, jsonAction.get(key).toString());
    }

    // initialise list
    _conditions = new ArrayList<Condition>();
    // grab conditions from json
    JSONArray jsonConditions = jsonAction.optJSONArray("conditions");

    // if we got some
    if (jsonConditions != null) {
        // loop them
        for (int i = 0; i < jsonConditions.length(); i++) {
            // add to our list
            _conditions.add(new Condition(jsonConditions.getJSONObject(i)));
        }
    }

    // get conditions type
    _conditionsType = jsonAction.getString("conditionsType");

    // grab any successActions
    JSONArray jsonTrueActions = jsonAction.optJSONArray("trueActions");
    // if we had some
    if (jsonTrueActions != null) {
        _trueActions = Control.getActions(rapidServlet, jsonTrueActions);
    }

    // grab any errorActions
    JSONArray jsonFalseActions = jsonAction.optJSONArray("falseActions");
    // if we had some
    if (jsonFalseActions != null) {
        // instantiate our contols collection
        _falseActions = Control.getActions(rapidServlet, jsonFalseActions);
    }

}

From source file:ch.ethz.inf.vs.android.g54.a4.util.SnapshotCache.java

private static List<WifiReading> jsonToReadings(JSONArray json) {
    List<WifiReading> readings = new ArrayList<WifiReading>(json.length());
    for (int i = 0; i < json.length(); i++) {
        try {/*from w  w  w. ja  va  2 s. c o  m*/
            JSONObject ap = json.getJSONObject(i);
            String mac = ap.getString("mac");
            String ssid = ap.getString("ssid");
            int signal = ap.getInt("signal");
            readings.add(new WifiReading(mac, ssid, signal));
        } catch (JSONException e) {
            return null;
        }
    }
    return readings;
}

From source file:curso.android.DAO.RespostaDAO.java

public static void insertLista(JSONArray jArray) {
    Resposta asw = null;/*  w  ww  . ja v  a 2  s  .c  om*/
    JSONObject json = null;
    try {
        for (int i = 0; i < jArray.length(); i++) {
            json = jArray.getJSONObject(i);
            asw = new Resposta();
            asw.setAsk_id(json.getLong("id"));
            asw.setAsk_id(json.getLong("pergunta"));
            asw.setUser_id(json.getLong("usuario"));
            asw.setUser_name(json.getString("user_name"));
            asw.setAsw_resposta(json.getString("resposta"));
            insert(asw);
            json = null;
            asw = null;
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

From source file:com.eventattend.portal.bl.FaceBookBL.java

public FaceBookDTO publicProfile(FaceBookDTO faceBookDTO) {
    FacebookJsonRestClient userClient = null;
    userClient = getUserClient((String) faceBookDTO.getAccessToken());
    EnumSet<ProfileField> fields1 = EnumSet.of(com.google.code.facebookapi.ProfileField.UID,
            com.google.code.facebookapi.ProfileField.NAME, com.google.code.facebookapi.ProfileField.PIC,
            com.google.code.facebookapi.ProfileField.ACTIVITIES,
            com.google.code.facebookapi.ProfileField.AFFILIATIONS,
            com.google.code.facebookapi.ProfileField.BIRTHDAY, com.google.code.facebookapi.ProfileField.BOOKS,
            com.google.code.facebookapi.ProfileField.EDUCATION_HISTORY,
            com.google.code.facebookapi.ProfileField.EMAIL_HASHES,
            com.google.code.facebookapi.ProfileField.HOMETOWN_LOCATION,
            com.google.code.facebookapi.ProfileField.HS_INFO,
            com.google.code.facebookapi.ProfileField.INTERESTS,
            com.google.code.facebookapi.ProfileField.WALL_COUNT,
            com.google.code.facebookapi.ProfileField.LOCALE, com.google.code.facebookapi.ProfileField.PIC_BIG,
            com.google.code.facebookapi.ProfileField.PIC_SMALL_WITH_LOGO,
            com.google.code.facebookapi.ProfileField.PROFILE_URL,
            com.google.code.facebookapi.ProfileField.QUOTES,
            com.google.code.facebookapi.ProfileField.RELATIONSHIP_STATUS,
            com.google.code.facebookapi.ProfileField.SEX, com.google.code.facebookapi.ProfileField.STATUS,
            com.google.code.facebookapi.ProfileField.CURRENT_LOCATION,
            com.google.code.facebookapi.ProfileField.FIRST_NAME,
            com.google.code.facebookapi.ProfileField.LAST_NAME,
            com.google.code.facebookapi.ProfileField.HOMETOWN_LOCATION,
            com.google.code.facebookapi.ProfileField.ABOUT_ME,
            com.google.code.facebookapi.ProfileField.WORK_HISTORY,
            com.google.code.facebookapi.ProfileField.PROFILE_URL,
            com.google.code.facebookapi.ProfileField.MOVIES,
            com.google.code.facebookapi.ProfileField.EMAIL_HASHES);
    long facebookUserID = Long.parseLong(faceBookDTO.getProfileId());
    JSONArray jsonArray = null;
    List currentUser = new ArrayList();
    currentUser.add(facebookUserID);//  w  w  w . ja va 2 s. c o m

    try {
        faceBookDTO = new FaceBookDTO();
        jsonArray = userClient.users_getInfo(currentUser, fields1);
        try {

            JSONObject obj = jsonArray.getJSONObject(0);
            faceBookDTO = getUserDetails(obj, faceBookDTO);

        } catch (JSONException e) {
            e.printStackTrace();
        }

    } catch (FacebookException e) {
        e.printStackTrace();
    }

    return faceBookDTO;
}

From source file:com.eventattend.portal.bl.FaceBookBL.java

public List personFBFriendsList(JSONArray jsonArray) {

    List friendsList = null;//from   w ww. j  a  v a  2 s . c o  m
    JSONObject jsonObject = null;
    friendsList = new ArrayList();

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

        try {
            jsonObject = jsonArray.getJSONObject(i);
            //    getUserDetails(jsonObject);

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
    return friendsList;

}

From source file:com.eventattend.portal.bl.FaceBookBL.java

/**
 * @param userClient/*from   w ww  .ja  va2  s .  c om*/
 * @return
 * @throws EventPortalException 
 */
private ProfileDTO getUserProfile(FacebookJsonRestClient userClient) throws EventPortalException {
    JSONArray jsonArray = null;
    ProfileDTO profileDTO = null;
    List currentUser = new ArrayList();
    EnumSet<ProfileField> fields1 = EnumSet.of(com.google.code.facebookapi.ProfileField.UID,
            com.google.code.facebookapi.ProfileField.NAME, com.google.code.facebookapi.ProfileField.PIC,
            com.google.code.facebookapi.ProfileField.CURRENT_LOCATION,
            com.google.code.facebookapi.ProfileField.FIRST_NAME,
            com.google.code.facebookapi.ProfileField.LAST_NAME,
            com.google.code.facebookapi.ProfileField.HOMETOWN_LOCATION,
            com.google.code.facebookapi.ProfileField.PROFILE_URL);
    try {

        Long facebookUserID = userClient.users_getLoggedInUser();
        currentUser.add(facebookUserID);

        jsonArray = userClient.users_getInfo(currentUser, fields1);

        try {

            JSONObject obj = jsonArray.getJSONObject(0);
            profileDTO = getUserData(obj);

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

    } catch (FacebookException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
        throw new EventPortalException("Facebook Session Invalid");

    }

    return profileDTO;
}