JAVA getlong例子_Java JSONObject.getLongValue方法代码示例

该代码展示了如何在Java中使用FastJSON库的JSONObject类来获取和处理Long值。通过遍历JSONArray,提取每个JSONObject中的"inode","pos"和"num"字段的Long值,并进行合法性检查。
摘要由CSDN通过智能技术生成

import com.alibaba.fastjson.JSONObject; //导入方法依赖的package包/类

public void loadPositions(String json) {

if (StringHelper.isEmpty(json)) {

return;

}

Long inode = 0L, pos = 0L, number = 0L;

String file = "";

JSONArray positionRecords = JSONArray.parseArray(json);

for (int i = 0; i < positionRecords.size(); i++) {

JSONObject positionObject = (JSONObject) positionRecords.get(i);

inode = positionObject.getLong("inode");

pos = positionObject.getLong("pos");

file = positionObject.getString("file");

// add line number

number = positionObject.getLongValue("num");

for (Object v : Arrays.asList(inode, pos, file)) {

Preconditions.checkNotNull(v, "Detected missing value in position file. " + "inode: " + inode

+ ", pos: " + pos + ", path: " + file);

}

TailFile tf = tailFiles.get(inode);

try {

if (tf != null && tf.updatePos(file, inode, pos, number)) {

tailFiles.put(inode, tf);

}

else {

// add old tail file into memory

maybeReloadMap.put(inode, new Long[] { pos, number });

if (log.isDebugEnable()) {

log.debug(this, "add old&inInterrupt file: " + file + ", inode: " + inode + ", pos: " + pos);

}

}

}

catch (IOException e) {

log.err(this, "TailFile updatePos FAILED.", e);

}

}

}

可以考虑使用以下方式进行优化: 1. 将查询操作放在子线程中执行,避免在主线程中执行数据库操作导致UI卡顿; 2. 使用缓存机制,将查询结果缓存起来,避免重复查询数据库; 3. 对于查询结果为空的情况,可以直接返回空的JSONArray,无需返回一个空的JSONArray对象; 4. 对于Cursor对象,最好在使用完之后进行关闭操作,以释放资源。 优化后的代码如下所示: @SuppressLint("Range") public void selectApmList(final Long sceneStartTime, final Callback<JSONArray> callback) { new Thread(new Runnable() { @Override public void run() { JSONArray apmJsonArray = new JSONArray(); String selection = "scene_start_time=?"; String[] selectionArgs = {String.valueOf(sceneStartTime)}; String orderBy = "_id"; Cursor cursor = mRDB.query(TABLE_PERFORMANCE, null, selection, selectionArgs, null, null, orderBy, null); if (cursor != null && cursor.getCount() > 0 && cursor.moveToFirst()) { int tsIndex = cursor.getColumnIndex("ts"); int pssIndex = cursor.getColumnIndex("pss"); int cpuIndex = cursor.getColumnIndex("cpu"); int currentIndex = cursor.getColumnIndex("current"); int voltageIndex = cursor.getColumnIndex("voltage"); int batteryPercentIndex = cursor.getColumnIndex("battery_percent"); do { JSONObject apmEntity = new JSONObject(); JsonUtils.put(apmEntity, ConfigConst.send_apmTsCollect, cursor.getLong(tsIndex)); JsonUtils.put(apmEntity, ConfigConst.send_apmPss, cursor.getLong(pssIndex)); JsonUtils.put(apmEntity, ConfigConst.send_apmCpu, cursor.getLong(cpuIndex)); JsonUtils.put(apmEntity, ConfigConst.send_apmCurrent, cursor.getLong(currentIndex)); JsonUtils.put(apmEntity, ConfigConst.send_apmVoltage, cursor.getLong(voltageIndex)); JsonUtils.put(apmEntity, ConfigConst.send_apmBatteryPercent, cursor.getLong(batteryPercentIndex)); apmJsonArray.put(apmEntity); } while (cursor.moveToNext()); callback.onSuccess(apmJsonArray); } else { callback.onSuccess(null); } if (cursor != null) { cursor.close(); } } }).start(); } 其中,Callback是自定义的回调接口,用于在查询完成后返回结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值