解释如下代码:public static List<String> t2(JSONObject json) { List<String> result = new ArrayList<>(); String success = json.getStr("success"); JSONObject jsonObject = json.getJSONObject("result"); JSONArray data = jsonObject.getJSONArray("data"); data.forEach(temp -> { JSONObject obj = (JSONObject)temp; String uniquekey = obj.getStr("uniquekey"); String title = obj.getStr("title"); String date = obj.getStr("date"); String category = obj.getStr("category"); String url = obj.getStr("url"); System.out.println(url); System.out.println(title); System.out.println(category); System.out.println(date); System.out.println(uniquekey); result.add(uniquekey); }); return result; }
时间: 2023-05-25 10:06:38 浏览: 98
这段代码定义了一个静态方法t2,它接收一个JSONObject对象作为参数。该方法首先创建一个空的ArrayList对象result用于存储后续处理得到的数据。接着从传入的JSONObject对象中获取一个名为success的字符串变量,并将其赋值给success变量。接下来从该对象中获取名为result的JSONObject对象,并将其赋值给jsonObject变量。然后从jsonObject中获取名为data的JSONArray数组对象,并将其赋值给data变量。接着对data数组中的每个元素执行forEach循环处理,将每个元素转换为JSONObject对象并获取其uniquekey、title、date、category和url等5个属性的值,并将这些值逐一输出到控制台上。最后将uniquekey值添加到result列表中,并最终返回该列表。
相关问题
List<JSONObject> 用JSONObject其中一个属性转成 List<String>
可以使用 Java 8 的 Stream API 将 List<JSONObject> 转换为 List<String>,具体代码如下:
```java
List<JSONObject> jsonList = ...; // 原始的 List<JSONObject>
List<String> stringList = jsonList.stream()
.map(json -> json.optString("propertyName")) // 将每个 JSONObject 转换为该对象的 "propertyName" 属性的值
.collect(Collectors.toList()); // 将转换后的结果收集到新的 List<String> 中
```
其中,"propertyName" 是你要转换为 List<String> 的 JSONObject 的属性名,可以根据实际情况进行修改。
public class DFALexer { public static boolean dfaRecognize(Map<String, Object> dfa, String inputStr) { return true } public static Map<String, Object> json_file2map(String fileName) { JSONParser parser = new JSONParser(); Map<String, Object> dfa = new HashMap<>(); try { Object obj = parser.parse(new FileReader(fileName)); JSONObject dfaJson = (JSONObject) obj; List<Integer> endStates = new ArrayList<>(); JSONArray endStatesJson = (JSONArray) dfaJson.get("end"); for (Object stateObj : endStatesJson) { endStates.add(((Long) stateObj).intValue()); } List<String> symbols = new ArrayList<>(); JSONArray symbolsJson = (JSONArray) dfaJson.get("syms"); for (Object symbolObj : symbolsJson) { symbols.add((String) symbolObj); } List<List<Integer>> transitions = new ArrayList<>(); JSONArray transitionsJson = (JSONArray) dfaJson.get("transfer"); for (Object transitionObj : transitionsJson) { JSONArray transitionJson = (JSONArray) transitionObj; List<Integer> transition = new ArrayList<>(); for (Object valueObj : transitionJson) { if (valueObj instanceof String) { transition.add((int) ((String) valueObj).charAt(0)); } else { transition.add(((Long) valueObj).intValue()); } } transitions.add(transition); } dfa.put("end", endStates); dfa.put("start", ((Long) dfaJson.get("start")).intValue()); dfa.put("syms", symbols); dfa.put("transfer", transitions); } catch (Exception e) { e.printStackTrace(); } return dfa; }在这段代码实现DFA识别方法,要求:inpuStr被成功接收则返回true,不成功接收则返回false
public class DFALexer {
public static boolean dfaRecognize(Map<String, Object> dfa, String inputStr) {
int currentState = (int)dfa.get("start");
List<List<Integer>> transitions = (List<List<Integer>>)dfa.get("transfer");
List<Integer> endStates = (List<Integer>)dfa.get("end");
List<String> symbols = (List<String>)dfa.get("syms");
for (int i = 0; i < inputStr.length(); i++) {
char c = inputStr.charAt(i);
int symbolIndex = symbols.indexOf(String.valueOf(c));
if (symbolIndex == -1) {
return false;
}
currentState = transitions.get(currentState).get(symbolIndex);
}
return endStates.contains(currentState);
}
public static Map<String, Object> json_file2map(String fileName) {
JSONParser parser = new JSONParser();
Map<String, Object> dfa = new HashMap<>();
try {
Object obj = parser.parse(new FileReader(fileName));
JSONObject dfaJson = (JSONObject) obj;
List<Integer> endStates = new ArrayList<>();
JSONArray endStatesJson = (JSONArray) dfaJson.get("end");
for (Object stateObj : endStatesJson) {
endStates.add(((Long) stateObj).intValue());
}
List<String> symbols = new ArrayList<>();
JSONArray symbolsJson = (JSONArray) dfaJson.get("syms");
for (Object symbolObj : symbolsJson) {
symbols.add((String) symbolObj);
}
List<List<Integer>> transitions = new ArrayList<>();
JSONArray transitionsJson = (JSONArray) dfaJson.get("transfer");
for (Object transitionObj : transitionsJson) {
JSONArray transitionJson = (JSONArray) transitionObj;
List<Integer> transition = new ArrayList<>();
for (Object valueObj : transitionJson) {
if (valueObj instanceof String) {
transition.add((int) ((String) valueObj).charAt(0));
} else {
transition.add(((Long) valueObj).intValue());
}
}
transitions.add(transition);
}
dfa.put("end", endStates);
dfa.put("start", ((Long) dfaJson.get("start")).intValue());
dfa.put("syms", symbols);
dfa.put("transfer", transitions);
} catch (Exception e) {
e.printStackTrace();
}
return dfa;
}
}
需要注意的是,这里的DFA是由一个JSON文件转换而来的,JSON文件应该包含以下属性:
- start:DFA的起始状态
- end:DFA的终止状态
- syms:DFA的输入字符集
- transfer:DFA的转移函数,用二维数组表示
如果JSON文件格式有误,会抛出异常并返回空的DFA。
阅读全文