Example usage for org.json JSONObject optLong

List of usage examples for org.json JSONObject optLong

Introduction

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

Prototype

public long optLong(String key, long defaultValue) 

Source Link

Document

Get an optional long value associated with a key, or the default if there is no such key or if the value is not a number.

Usage

From source file:app.sunstreak.yourpisd.net.Student.java

public boolean hasClassGrade(int classIndex, int termIndex) throws JSONException {

    int termIndexOffset = 0;
    if (gradeSummary[classIndex][3] == CLASS_DISABLED_DURING_TERM)
        termIndexOffset = 4;/*w  w  w  . j a v  a 2 s .  c o m*/

    termIndex -= termIndexOffset;

    if (classGrades.indexOfKey(classIndex) < 0)
        return false;

    JSONObject classGrade = classGrades.get(classIndex);
    JSONArray terms = classGrade.getJSONArray("terms");
    JSONObject term = terms.getJSONObject(termIndex);
    long lastUpdated = term.optLong("lastUpdated", -1);

    return lastUpdated != -1;
}

From source file:ezy.boost.update.UpdateInfo.java

private static UpdateInfo parse(JSONObject o) {
    UpdateInfo info = new UpdateInfo();
    if (o == null) {
        return info;
    }/*from  w ww  . j  a v a 2  s. c om*/
    info.hasUpdate = o.optBoolean("hasUpdate", false);
    if (!info.hasUpdate) {
        return info;
    }
    info.isSilent = o.optBoolean("isSilent", false);
    info.isForce = o.optBoolean("isForce", false);
    info.isAutoInstall = o.optBoolean("isAutoInstall", !info.isSilent);
    info.isIgnorable = o.optBoolean("isIgnorable", true);
    info.isPatch = o.optBoolean("isPatch", false);

    info.versionCode = o.optInt("versionCode", 0);
    info.versionName = o.optString("versionName");
    info.updateContent = o.optString("updateContent");

    info.url = o.optString("url");
    info.md5 = o.optString("md5");
    info.size = o.optLong("size", 0);

    if (!info.isPatch) {
        return info;
    }
    info.patchUrl = o.optString("patchUrl");
    info.patchMd5 = o.optString("patchMd5");
    info.patchSize = o.optLong("patchSize", 0);
    return info;
}

From source file:org.andstatus.app.backup.MyBackupDescriptor.java

static MyBackupDescriptor fromOldParcelFileDescriptor(ParcelFileDescriptor parcelFileDescriptor,
        ProgressLogger progressLogger) {
    MyBackupDescriptor myBackupDescriptor = new MyBackupDescriptor(progressLogger);
    if (parcelFileDescriptor != null) {
        myBackupDescriptor.fileDescriptor = parcelFileDescriptor.getFileDescriptor();
        JSONObject jso = FileDescriptorUtils.getJSONObject(parcelFileDescriptor.getFileDescriptor());
        myBackupDescriptor.backupSchemaVersion = jso.optInt(KEY_BACKUP_SCHEMA_VERSION,
                myBackupDescriptor.backupSchemaVersion);
        myBackupDescriptor.createdDate = jso.optLong(KEY_CREATED_DATE, myBackupDescriptor.createdDate);
        myBackupDescriptor.applicationVersionCode = jso.optInt(KEY_APPLICATION_VERSION_CODE,
                myBackupDescriptor.applicationVersionCode);
        myBackupDescriptor.accountsCount = jso.optLong(KEY_ACCOUNTS_COUNT, myBackupDescriptor.accountsCount);
        if (myBackupDescriptor.backupSchemaVersion != BACKUP_SCHEMA_VERSION) {
            try {
                MyLog.w(TAG, "Bad backup descriptor: " + jso.toString(2));
            } catch (JSONException e) {
                MyLog.d(TAG, "Bad backup descriptor: " + jso.toString(), e);
            }/*from w  w  w  .  j ava 2 s  .  c  o m*/
        }
    }
    return myBackupDescriptor;
}

From source file:net.geco.model.iojson.PersistentStore.java

public void importCourses(JSONStore store, Registry registry, Factory factory) throws JSONException {
    JSONArray courses = store.getJSONArray(K.COURSES);
    for (int i = 0; i < courses.length(); i++) {
        JSONObject c = courses.getJSONObject(i);
        Course course = store.register(factory.createCourse(), c.getInt(K.ID));
        course.setName(c.getString(K.NAME));
        course.setLength(c.getInt(K.LENGTH));
        course.setClimb(c.getInt(K.CLIMB));
        course.setMassStartTime(new Date(c.optLong(K.START, TimeManager.NO_TIME_l))); // MIGR v2.x -> v2.2
        course.setCourseSet(store.retrieve(c.optInt(K.COURSESET, 0), CourseSet.class));
        JSONArray codez = c.getJSONArray(K.CODES);
        int[] codes = new int[codez.length()];
        for (int j = 0; j < codes.length; j++) {
            codes[j] = codez.getInt(j);/*from www  .j  a  va  2  s  . com*/
        }
        course.setCodes(codes);
        if (c.has(K.SECTIONS)) {
            JSONArray sectionz = c.getJSONArray(K.SECTIONS);
            for (int j = 0; j < sectionz.length(); j++) {
                JSONObject sectionTuple = sectionz.getJSONObject(j);
                Section section = store.register(factory.createSection(), sectionTuple.getInt(K.ID));
                section.setStartIndex(sectionTuple.getInt(K.START_ID));
                section.setName(sectionTuple.getString(K.NAME));
                section.setType(SectionType.valueOf(sectionTuple.getString(K.TYPE)));
                section.setNeutralized(sectionTuple.optBoolean(K.NEUTRALIZED, false));
                course.putSection(section);
            }
            course.refreshSectionCodes();
        }
        registry.addCourse(course);
    }
    registry.ensureAutoCourse(factory);
}

From source file:net.geco.model.iojson.PersistentStore.java

public void importRunnersData(JSONStore store, Registry registry, Factory factory) throws JSONException {
    final int I_RUNNER = 0;
    final int I_ECARD = 1;
    final int I_RESULT = 2;
    JSONArray runnersData = store.getJSONArray(K.RUNNERS_DATA);
    for (int i = 0; i < runnersData.length(); i++) {
        JSONArray runnerTuple = runnersData.getJSONArray(i);

        JSONObject c = runnerTuple.getJSONObject(I_RUNNER);
        Runner runner = factory.createRunner();
        runner.setStartId(c.getInt(K.START_ID));
        runner.setFirstname(c.getString(K.FIRST));
        runner.setLastname(c.getString(K.LAST));
        runner.setEcard(c.getString(K.ECARD));
        runner.setClub(store.retrieve(c.getInt(K.CLUB), Club.class));
        runner.setCategory(store.retrieve(c.getInt(K.CAT), Category.class));
        runner.setCourse(store.retrieve(c.getInt(K.COURSE), Course.class));
        runner.setRegisteredStarttime(new Date(c.getLong(K.START)));
        runner.setArchiveId((Integer) c.opt(K.ARK));
        runner.setRentedEcard(c.optBoolean(K.RENT));
        runner.setNC(c.optBoolean(K.NC));
        registry.addRunner(runner);//  www.  ja  v a 2 s.c o m

        JSONObject d = runnerTuple.getJSONObject(I_ECARD);
        RunnerRaceData raceData = factory.createRunnerRaceData();
        raceData.setStarttime(new Date(d.getLong(K.START)));
        raceData.setFinishtime(new Date(d.getLong(K.FINISH)));
        raceData.setControltime(new Date(d.getLong(K.CHECK)));
        raceData.setReadtime(new Date(d.getLong(K.READ)));
        JSONArray p = d.getJSONArray(K.PUNCHES);
        Punch[] punches = new Punch[p.length() / 2];
        for (int j = 0; j < punches.length; j++) {
            punches[j] = factory.createPunch();
            punches[j].setCode(p.getInt(2 * j));
            punches[j].setTime(new Date(p.getLong(2 * j + 1)));
        }
        raceData.setPunches(punches);
        raceData.setRunner(runner);
        registry.addRunnerData(raceData);

        JSONObject r = runnerTuple.getJSONObject(I_RESULT);
        TraceData traceData = factory.createTraceData();
        traceData.setNbMPs(r.getInt(K.MPS));
        traceData.setNbExtraneous(r.optInt(K.EXTRA)); // MIGR v2.x -> v2.3
        JSONArray t = r.getJSONArray(K.TRACE);
        Trace[] trace = new Trace[t.length() / 2];
        for (int j = 0; j < trace.length; j++) {
            trace[j] = factory.createTrace(t.getString(2 * j), new Date(t.getLong(2 * j + 1)));
        }
        if (r.has(K.SECTION_DATA)) {
            SectionTraceData sectionData = (SectionTraceData) traceData;
            JSONArray sections = r.getJSONArray(K.SECTION_DATA);
            for (int j = 0; j < sections.length(); j++) {
                JSONArray section = sections.getJSONArray(j);
                sectionData.putSectionAt(store.retrieve(section.getInt(0), Section.class), section.getInt(1));
            }
        }
        JSONArray neut = r.getJSONArray(K.NEUTRALIZED);
        for (int j = 0; j < neut.length(); j++) {
            trace[neut.getInt(j)].setNeutralized(true);
        }
        traceData.setTrace(trace);
        raceData.setTraceData(traceData);

        RunnerResult result = factory.createRunnerResult();
        result.setRaceTime(r.optLong(K.RACE_TIME, TimeManager.NO_TIME_l)); // MIGR v2.x -> v2.2
        result.setResultTime(r.getLong(K.TIME));
        result.setStatus(Status.valueOf(r.getString(K.STATUS)));
        result.setTimePenalty(r.getLong(K.PENALTY));
        result.setManualTimePenalty(r.optLong(K.MANUAL_PENALTY, 0)); // MIGR v2.x -> v2.3
        raceData.setResult(result);
    }
}

From source file:com.phonegap.Capture.java

@Override
public PluginResult execute(String action, JSONArray args, String callbackId) {
    this.callbackId = callbackId;
    this.limit = 1;
    this.duration = 0.0f;
    this.results = new JSONArray();

    JSONObject options = args.optJSONObject(0);
    if (options != null) {
        limit = options.optLong("limit", 1);
        duration = options.optDouble("duration", 0.0f);
    }/*from  w w  w. j  a va2 s.  c o m*/

    if (action.equals("getFormatData")) {
        try {
            JSONObject obj = getFormatData(args.getString(0), args.getString(1));
            return new PluginResult(PluginResult.Status.OK, obj);
        } catch (JSONException e) {
            return new PluginResult(PluginResult.Status.ERROR);
        }
    } else if (action.equals("captureAudio")) {
        this.captureAudio();
    } else if (action.equals("captureImage")) {
        this.captureImage();
    } else if (action.equals("captureVideo")) {
        this.captureVideo(duration);
    }

    PluginResult r = new PluginResult(PluginResult.Status.NO_RESULT);
    r.setKeepCallback(true);
    return r;
}

From source file:dlauncher.modpacks.download.DefaultDownloadLocation.java

public static DownloadLocation valueOf(JSONObject obj) throws MalformedURLException, JSONException {
    byte[] md5 = null;
    byte[] sha512 = null;
    long fileLength = -1;
    String optMd5 = obj.optString("md5", null);
    String optSha512 = obj.optString("sha512", null);
    if (optMd5 != null) {
        md5 = hexStringToByteArray(optMd5);
    }/*  w  ww.  ja va2  s . co m*/
    if (optSha512 != null) {
        sha512 = hexStringToByteArray(optSha512);
    }
    fileLength = obj.optLong("size", fileLength);
    String urlStr = obj.getString("url");
    URL url;
    if (urlStr.startsWith("default:")) {
        url = DefaultDownloadLocation.class.getResource(urlStr.substring("default:".length()));
    } else {
        url = new URL(urlStr);
    }
    return new DefaultDownloadLocation(url, fileLength, md5, sha512);
}

From source file:com.linkedin.platform.errors.ApiErrorResponse.java

public static ApiErrorResponse build(JSONObject jsonErr) throws JSONException {
    return new ApiErrorResponse(jsonErr, jsonErr.optInt(ERROR_CODE, -1), jsonErr.optString(MESSAGE),
            jsonErr.optString(REQUEST_ID), jsonErr.optInt(STATUS, -1), jsonErr.optLong(TIMESTAMP, 0));
}

From source file:at.alladin.rmbt.android.views.ResultDetailsView.java

@Override
public void taskEnded(final JSONArray testResultDetail) {
    //if (getVisibility()!=View.VISIBLE)
    //    return;

    if (this.resultFetchEndTaskListener != null) {
        this.resultFetchEndTaskListener.taskEnded(testResultDetail);
    }//from w ww. java 2  s.c  o m

    if (testResultDetail != null && testResultDetail.length() > 0
            && (testResultDetailTask == null || !testResultDetailTask.hasError())) {
        this.testResult = testResultDetail;

        System.out.println("testResultDetail: " + testResultDetail);

        try {

            HashMap<String, String> viewItem;

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

                final JSONObject singleItem = testResultDetail.getJSONObject(i);

                viewItem = new HashMap<String, String>();
                viewItem.put("name", singleItem.optString("title", ""));

                if (singleItem.has("time")) {
                    final String timeString = Helperfunctions.formatTimestampWithTimezone(
                            singleItem.optLong("time", 0), singleItem.optString("timezone", null),
                            true /* seconds */);
                    viewItem.put("value", timeString == null ? "-" : timeString);
                } else
                    viewItem.put("value", singleItem.optString("value", ""));
                itemList.add(viewItem);
            }
        } catch (final JSONException e) {
            e.printStackTrace();
        }

        valueList = new SimpleAdapter(activity, itemList, R.layout.test_result_detail_item,
                new String[] { "name", "value" }, new int[] { R.id.name, R.id.value });

        listView.setAdapter(valueList);

        listView.invalidate();

        progessBar.setVisibility(View.GONE);
        emptyView.setVisibility(View.GONE);
        listView.setVisibility(View.VISIBLE);

    } else {
        Log.i(DEBUG_TAG, "LEERE LISTE");
        progessBar.setVisibility(View.GONE);
        emptyView.setVisibility(View.VISIBLE);
        emptyView.setText(activity.getString(R.string.error_no_data));
        emptyView.invalidate();
    }

}

From source file:org.eclipse.orion.internal.server.servlets.file.ServletFileStoreHandler.java

/**
 * Copies any defined fields in the provided JSON object into the destination file info.
 * @param source The JSON object to copy fields from
 * @param destination The file info to copy fields to
 */// w ww.  ja  va  2  s  .  co  m
public static void copyJSONToFileInfo(JSONObject source, FileInfo destination) {
    destination.setName(source.optString(ProtocolConstants.KEY_NAME, destination.getName()));
    destination.setLastModified(
            source.optLong(ProtocolConstants.KEY_LAST_MODIFIED, destination.getLastModified()));
    destination.setDirectory(source.optBoolean(ProtocolConstants.KEY_DIRECTORY, destination.isDirectory()));

    JSONObject attributes = source.optJSONObject(ProtocolConstants.KEY_ATTRIBUTES);
    if (attributes != null) {
        for (int i = 0; i < ATTRIBUTE_KEYS.length; i++) {
            //undefined means the client does not want to change the value, so can't interpret as false
            if (!attributes.isNull(ATTRIBUTE_KEYS[i]))
                destination.setAttribute(ATTRIBUTE_BITS[i], attributes.optBoolean(ATTRIBUTE_KEYS[i]));
        }
    }
}