Example usage for org.json JSONObject getInt

List of usage examples for org.json JSONObject getInt

Introduction

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

Prototype

public int getInt(String key) throws JSONException 

Source Link

Document

Get the int value associated with a key.

Usage

From source file:com.sepgil.ral.Node.java

/**
 * Creates a Node out of a JSON object./*ww  w.  j  ava2  s. c o  m*/
 * @param object The JSON object that represents the node.
 * @throws JSONException Can be thrown when a field is missing.
 */
public Node(JSONObject object) throws JSONException {
    mNid = object.getInt("nid");
    mTitle = object.getString("title");
    mBody = object.getJSONObject("body").getString("value");
}

From source file:org.loklak.server.AbstractAPIHandler.java

private void process(HttpServletRequest request, HttpServletResponse response, Query query)
        throws ServletException, IOException {

    // basic protection
    BaseUserRole minimalBaseUserRole = getMinimalBaseUserRole() != null ? getMinimalBaseUserRole()
            : BaseUserRole.ANONYMOUS;/* w w w  . j  ava 2s  .  c  om*/

    if (query.isDoS_blackout()) {
        response.sendError(503, "your request frequency is too high");
        return;
    } // DoS protection
    if (DAO.getConfig("users.admin.localonly", true) && minimalBaseUserRole == BaseUserRole.ADMIN
            && !query.isLocalhostAccess()) {
        response.sendError(503,
                "access only allowed from localhost, your request comes from " + query.getClientHost());
        return;
    } // danger! do not remove this!

    // user identification
    ClientIdentity identity = getIdentity(request, response, query);

    // user authorization: we use the identification of the user to get the assigned authorization
    Authorization authorization = new Authorization(identity, DAO.authorization, DAO.userRoles);

    if (authorization.getBaseUserRole().ordinal() < minimalBaseUserRole.ordinal()) {
        response.sendError(401,
                "Base user role not sufficient. Your base user role is '"
                        + authorization.getBaseUserRole().name() + "', your user role is '"
                        + authorization.getUserRole().getDisplayName() + "'");
        return;
    }

    // user accounting: we maintain static and persistent user data; we again search the accounts using the usder identity string
    //JSONObject accounting_persistent_obj = DAO.accounting_persistent.has(user_id) ? DAO.accounting_persistent.getJSONObject(anon_id) : DAO.accounting_persistent.put(user_id, new JSONObject()).getJSONObject(user_id);
    Accounting accounting_temporary = DAO.accounting_temporary.get(identity.toString());
    if (accounting_temporary == null) {
        accounting_temporary = new Accounting();
        DAO.accounting_temporary.put(identity.toString(), accounting_temporary);
    }

    // the accounting data is assigned to the authorization
    authorization.setAccounting(accounting_temporary);

    // extract standard query attributes
    String callback = query.get("callback", "");
    boolean jsonp = callback.length() > 0;
    boolean minified = query.get("minified", false);

    try {
        JSONObject json = serviceImpl(query, response, authorization, authorization.getPermissions(this));
        if (json == null) {
            response.sendError(400, "your request does not contain the required data");
            return;
        }

        // evaluate special fields
        if (json.has("$EXPIRES")) {
            int expires = json.getInt("$EXPIRES");
            FileHandler.setCaching(response, expires);
            json.remove("$EXPIRES");
        }

        // add session information
        JSONObject session = new JSONObject(true);
        session.put("identity", identity.toJSON());
        json.put("session", session);

        // write json
        query.setResponse(response, "application/javascript");
        response.setCharacterEncoding("UTF-8");
        PrintWriter sos = response.getWriter();
        if (jsonp)
            sos.print(callback + "(");
        sos.print(json.toString(minified ? 0 : 2));
        if (jsonp)
            sos.println(");");
        sos.println();
        query.finalize();
    } catch (APIException e) {
        response.sendError(e.getStatusCode(), e.getMessage());
        return;
    }
}

From source file:com.github.sommeri.sourcemap.SourceMapConsumerV3.java

/**
 * Parses the given contents containing a source map.
 *//*from w  w w  .j  a  v a 2s . c  o  m*/
public void parse(JSONObject sourceMapRoot, SourceMapSupplier sectionSupplier) throws SourceMapParseException {
    try {
        // Check basic assertions about the format.
        int version = sourceMapRoot.getInt("version");
        if (version != 3) {
            throw new SourceMapParseException("Unknown version: " + version);
        }

        this.file = sourceMapRoot.getString("file");
        if (file.isEmpty()) {
            //SMS: (source map separation):  commented this - I need more tolerant parser
            //throw new SourceMapParseException("File entry is missing or empty ");
        }
        if (sourceMapRoot.has("sourceRoot"))
            this.sourceRoot = sourceMapRoot.getString("sourceRoot");

        if (sourceMapRoot.has("sections")) {
            // Looks like a index map, try to parse it that way.
            parseMetaMap(sourceMapRoot, sectionSupplier);
            return;
        }

        lineCount = sourceMapRoot.getInt("lineCount");
        String lineMap = sourceMapRoot.getString("mappings");

        sources = getJavaStringArray(sourceMapRoot.getJSONArray("sources"));
        if (sourceMapRoot.has("sourcesContent")) {
            sourcesContent = getJavaStringArray(sourceMapRoot.getJSONArray("sourcesContent"));
        } else {
            sourcesContent = new String[sources.length];
        }
        names = getJavaStringArray(sourceMapRoot.getJSONArray("names"));

        lines = new ArrayList<ArrayList<Entry>>(lineCount);

        new MappingBuilder(lineMap).build();
    } catch (JSONException ex) {
        throw new SourceMapParseException("JSON parse exception: " + ex);
    }
}

From source file:com.github.sommeri.sourcemap.SourceMapConsumerV3.java

/**
 * @param sourceMapRoot//from  w w w.ja v  a  2  s.  com
 * @throws SourceMapParseException
 */
private void parseMetaMap(JSONObject sourceMapRoot, SourceMapSupplier sectionSupplier)
        throws SourceMapParseException {
    if (sectionSupplier == null) {
        sectionSupplier = new DefaultSourceMapSupplier();
    }

    try {
        // Check basic assertions about the format.
        int version = sourceMapRoot.getInt("version");
        if (version != 3) {
            throw new SourceMapParseException("Unknown version: " + version);
        }

        String file = sourceMapRoot.getString("file");
        if (file.isEmpty()) {
            throw new SourceMapParseException("File entry is missing or empty");
        }

        if (sourceMapRoot.has("lineCount") || sourceMapRoot.has("mappings") || sourceMapRoot.has("sources")
                || sourceMapRoot.has("names")) {
            throw new SourceMapParseException("Invalid map format");
        }

        SourceMapGeneratorV3 generator = new SourceMapGeneratorV3();
        JSONArray sections = sourceMapRoot.getJSONArray("sections");
        for (int i = 0, count = sections.length(); i < count; i++) {
            JSONObject section = sections.getJSONObject(i);
            if (section.has("map") && section.has("url")) {
                throw new SourceMapParseException(
                        "Invalid map format: section may not have both 'map' and 'url'");
            }
            JSONObject offset = section.getJSONObject("offset");
            int line = offset.getInt("line");
            int column = offset.getInt("column");
            String mapSectionContents;
            if (section.has("url")) {
                String url = section.getString("url");
                mapSectionContents = sectionSupplier.getSourceMap(url);
                if (mapSectionContents == null) {
                    throw new SourceMapParseException("Unable to retrieve: " + url);
                }
            } else if (section.has("map")) {
                mapSectionContents = section.getString("map");
            } else {
                throw new SourceMapParseException(
                        "Invalid map format: section must have either 'map' or 'url'");
            }
            generator.mergeMapSection(line, column, mapSectionContents);
        }

        StringBuilder sb = new StringBuilder();
        try {
            generator.appendTo(sb, file);
        } catch (IOException e) {
            // Can't happen.
            throw new RuntimeException(e);
        }

        parse(sb.toString());
    } catch (IOException ex) {
        throw new SourceMapParseException("IO exception: " + ex);
    } catch (JSONException ex) {
        throw new SourceMapParseException("JSON parse exception: " + ex);
    }
}

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

public static ArrayList<Person> parseResult(String wsResponse) throws DataException {
    ArrayList<Person> personList = new ArrayList<Person>();

    try {/*from  w  ww .ja v a 2 s .  co m*/
        JSONObject parser = new JSONObject(wsResponse);
        JSONObject jsonRoot = parser.getJSONObject(JSONTag.PERSON_LIST_ELEM_PERSONS);
        JSONArray jsonPersonArray = jsonRoot.getJSONArray(JSONTag.PERSON_LIST_ELEM_PERSON);
        int size = jsonPersonArray.length();
        for (int i = 0; i < size; i++) {
            JSONObject jsonPerson = jsonPersonArray.getJSONObject(i);
            Person person = new Person();

            person.firstName = jsonPerson.getString(JSONTag.PERSON_LIST_ELEM_PERSON_FIRST_NAME);
            person.lastName = jsonPerson.getString(JSONTag.PERSON_LIST_ELEM_PERSON_LAST_NAME);
            person.email = jsonPerson.getString(JSONTag.PERSON_LIST_ELEM_PERSON_EMAIL);
            person.city = jsonPerson.getString(JSONTag.PERSON_LIST_ELEM_PERSON_CITY);
            person.postalCode = jsonPerson.getInt(JSONTag.PERSON_LIST_ELEM_PERSON_POSTAL_CODE);
            person.age = jsonPerson.getInt(JSONTag.PERSON_LIST_ELEM_PERSON_AGE);
            person.isWorking = jsonPerson.getInt(JSONTag.PERSON_LIST_ELEM_PERSON_IS_WORKING) == 1;

            personList.add(person);
        }
    } catch (JSONException e) {
        Log.e(TAG, "JSONException", e);
        throw new DataException(e);
    }

    return personList;
}

From source file:com.norman0406.slimgress.API.Knobs.PortalKnobs.java

public PortalKnobs(JSONObject json) throws JSONException {
    super(json);//  www.j a va 2 s .  c om

    JSONObject resonatorLimits = json.getJSONObject("resonatorLimits");
    JSONArray bands = resonatorLimits.getJSONArray("bands");
    mBands = new ArrayList<Band>();
    for (int i = 0; i < bands.length(); i++) {
        JSONObject band = bands.getJSONObject(i);
        Band newBand = new Band();
        newBand.applicableLevels = getIntArray(band, "applicableLevels");
        newBand.remaining = band.getInt("remaining");
        mBands.add(newBand);
    }

    mCanPlayerRemoveMod = json.getBoolean("canPlayerRemoveMod");
    mMaxModsPerPlayer = json.getInt("maxModsPerPlayer");
}

From source file:org.mozilla.gecko.gfx.IntRect.java

public IntRect(JSONObject json) {
    try {//from  www.  j a v a2 s. c  o  m
        x = json.getInt("x");
        y = json.getInt("y");
        width = json.getInt("width");
        height = json.getInt("height");
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }
}

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

public static ArrayList<Phone> parseResult(String wsResponse) throws DataException {
    ArrayList<Phone> phoneList = new ArrayList<Phone>();

    try {//from  w  w w  .  ja va  2 s.  c o  m
        JSONObject parser = new JSONObject(wsResponse);
        JSONObject jsonRoot = parser.getJSONObject(JSONTag.CRUD_PHONE_LIST_ELEM_PHONES);
        JSONArray jsonPhoneArray = jsonRoot.getJSONArray(JSONTag.CRUD_PHONE_LIST_ELEM_PHONE);
        int size = jsonPhoneArray.length();
        for (int i = 0; i < size; i++) {
            JSONObject jsonPhone = jsonPhoneArray.getJSONObject(i);
            Phone phone = new Phone();

            phone.serverId = jsonPhone.getLong(JSONTag.CRUD_PHONE_LIST_ELEM_ID);
            phone.name = jsonPhone.getString(JSONTag.CRUD_PHONE_LIST_ELEM_NAME);
            phone.manufacturer = jsonPhone.getString(JSONTag.CRUD_PHONE_LIST_ELEM_MANUFACTURER);
            phone.androidVersion = jsonPhone.getString(JSONTag.CRUD_PHONE_LIST_ELEM_ANDROID_VERSION);
            phone.screenSize = jsonPhone.getDouble(JSONTag.CRUD_PHONE_LIST_ELEM_SCREEN_SIZE);
            phone.price = jsonPhone.getInt(JSONTag.CRUD_PHONE_LIST_ELEM_PRICE);

            phoneList.add(phone);
        }
    } catch (JSONException e) {
        Log.e(TAG, "JSONException", e);
        throw new DataException(e);
    }

    return phoneList;
}

From source file:com.nextgis.firereporter.ScanexNotificationItem.java

public ScanexNotificationItem(Context c, JSONObject object) {
    Prepare(c);//from ww  w . ja  v a 2 s .c o  m
    try {
        this.nID = object.getLong("id");
        this.dt = new Date(object.getLong("date"));
        this.X = object.getDouble("X");
        this.Y = object.getDouble("Y");
        this.nConfidence = object.getInt("confidence");
        this.nPower = object.getInt("power");
        this.sURL1 = object.getString("URL1");
        this.sURL2 = object.getString("URL2");
        this.sType = object.getString("type");
        this.sPlace = object.getString("place");
        this.sMap = object.getString("map");
        this.nIconId = object.getInt("iconId");
        this.mbWatched = object.getBoolean("watched");

    } catch (JSONException e) {
        SendError(e.getLocalizedMessage());
    }

}

From source file:com.tassadar.multirommgr.installfragment.UbuntuImage.java

public UbuntuImage(JSONObject img) throws JSONException {
    version = img.getInt("version");
    description = img.getString("description");

    JSONArray f = img.getJSONArray("files");
    for (int i = 0; i < f.length(); ++i) {
        JSONObject file = f.getJSONObject(i);
        files.add(new UbuntuFile(file));
    }//  w  w  w .j  a  va 2s .c o m

    Collections.sort(files, this);
}