[英]Unable to parse json object from log file in java
我正在學習Java。 我有一個示例日志文件,其中包含密鑰對及其值。 我想從某些行中找到一個密鑰對及其價值。 例如,如果行以“ x”開頭,請讀取該行並找到它。 我能夠獲取密鑰對及其在輸出文件中的值,但無法獲取行尾的json對象。 目標是將文本文件作為輸入,並查找句子是否以特定單詞開頭。 如果是,請從該行中查找鍵對和值,並包含json對象(如果有)
任務:查找以“ [Student info]”開頭的行,並解析學生ID,班級狀態,輸入(這是json對象)。 有時輸入也可能為空。
我已經在該平台上檢查了其他問題,包括此問題,但並沒有太大幫助。
**Input file.**
[King] 9 AM America -- kingNumber 1234567890 -- address: abc/cd/ef -- birthmonth: unknown Input={||Ouptut={}
[born time] 9 AM London -- kingNumber 1234567890 -- address: abc/cd/ef -- birthmonth: unknown Input={||Ouptut={}
[Student info] -- 12/08/2011 -- StudentId: 124421 -- Phonenumber: 4252076406 -- ClassStatus: Senior -- "Random String..." Input={ || Ouput={}
[born time] 9 AM London -- kingNumber 1234567890 -- address: abc/cd/ef -- birthmonth: unknown Input={||Ouptut={}
[Student info] -- 12/08/2011 -- StudentId: 1234567 -- Phonenumber: 4252076406 -- ClassStatus: Senior -- "Random String..." Input={
"kaju": [{
"Sno": {
"type": "literal",
"value": "random"
}
}]
}
[Game] 9 AM London -- kingNumber 1234567890 -- address: abc/cd/ef -- birthmonth: unknown Input={
[Student info] -- 12/08/2011 -- StudentId: 1234567 -- Phonenumber: 4252076406 -- ClassStatus: Senior -- "Random String..." Input={ || Ouput={
"core": [{
"id": {
"datatype": "https://www.w3schools.com/",
"type": "website",
"value": "study"
},
"entity": {
"type": "url",
"value": "https://www.w3schools.com/"
},
"Sno": {
"type": "literal",
"value": "random"
}
}]
}
**Expected Output File**
[Student info] -- 12/08/2011 -- StudentId: 124421 -- Phonenumber: 4252076406 -- ClassStatus: Senior -- "Random String..." Input={ || Ouput={}
[Student info] -- 12/08/2011 -- StudentId: 1234567 -- Phonenumber: 4252076406 -- ClassStatus: Senior -- "Random String..." Input={
"kaju": [{
"Sno": {
"type": "literal",
"value": "random"
}
}]
}
[Student info] -- 12/08/2011 -- StudentId: 1234567 -- Phonenumber: 4252076406 -- ClassStatus: Senior -- "Random String..." Input={ || Ouput={
"core": [{
"id": {
"datatype": "https://www.w3schools.com/",
"type": "website",
"value": "study"
},
"entity": {
"type": "url",
"value": "https://www.w3schools.com/"
},
"Sno": {
"type": "literal",
"value": "random"
}
}]
}
**Current Output File**
[Student info] -- 12/08/2011 -- StudentId: 124421 -- Phonenumber: 4252076406 -- ClassStatus: Senior -- "Random String..." Input={ || Ouput={}
[Student info] -- 12/08/2011 -- StudentId: 1234567 -- Phonenumber: 4252076406 -- ClassStatus: Senior -- "Random String..." Input={
[Student info] -- 12/08/2011 -- StudentId: 1234567 -- Phonenumber: 4252076406 -- ClassStatus: Senior -- "Random String..." Input={ || Ouput={
從上面的文本文件中可以看到,我只能提取所需的行(學生信息),但無法獲取附加的json對象。 我也希望抽象json對象(如果有)。 在這種情況下,新行始終以“ [”開頭。
注意:
下面是我的代碼。
public class Kickstarter {
public static void main (String... args) {
Scanner scan = new Scanner(Kickstarter.class.getResourceAsStream("sample.txt"));
List<String> fileByLine = new ArrayList<>();
List<StudentInfo> result = new ArrayList<>();
while(scan.hasNextLine()) {
fileByLine.add(scan.nextLine());
}
for (Iterator<String> iterator = fileByLine.iterator(); iterator.hasNext();) {
String line = iterator.next();
if (!line.startsWith("[Student info]")) {
iterator.remove();
}
}
for (int i = 0; i < fileByLine.size(); i++) {
System.out.println(fileByLine.get(i));
}
}
}
另一個檔案
import com.google.gson.JsonObject;
public class StudentInfo {
public String studentinfo;
public boolean isBasic;
public JsonObject json;
public String jsonStringBuildUp;
public StudentInfo (String studentinfo, boolean isBasic) {
this.studentinfo = studentinfo;
this.isBasic = isBasic;
this.json = null;
this.jsonStringBuildUp = "";
}
}
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
import com.google.gson.Gson;
public class KickStarter {
private static final String STUDENT_INFO = "[Student info]";
private Map<String, StudentInfo> lineJsonMap;
private String key;
private StringBuilder jsonBuilder;
public static void main(String... args) {
new KickStarter().scan();
}
private void scan() {
Scanner scan = new Scanner(KickStarter.class.getClassLoader().getResourceAsStream("sample.txt"));
lineJsonMap = new HashMap<String, StudentInfo>();
jsonBuilder = null;
key = null;
while (scan.hasNextLine()) {
String line = scan.nextLine();
if (!line.startsWith("[")) {
if (jsonBuilder == null) {
jsonBuilder = new StringBuilder();
jsonBuilder.append("{");
}
jsonBuilder.append(line.trim());
} else if (line.startsWith(STUDENT_INFO)) {
extractJSON();
lineJsonMap.put(line, null);
key = line;
} else if (line.startsWith("[")) {
extractJSON();
}
}
extractJSON();
scan.close();
// Finally Print values
Set<Entry<String, StudentInfo>> entrySet = lineJsonMap.entrySet();
for (Entry<String, StudentInfo> entry : entrySet) {
System.out.println(entry.getKey());
StudentInfo value = entry.getValue();
if (value != null) {
Gson gson = new Gson();
System.out.println(gson.toJson(value));
}
}
}
private void extractJSON() {
if (key != null) {
StudentInfo fromJson = null;
if (jsonBuilder != null) {
Gson gson = new Gson();
fromJson = gson.fromJson(jsonBuilder.toString(), StudentInfo.class);
}
lineJsonMap.put(key, fromJson);
key = null;
jsonBuilder = null;
}
}
}
您需要創建如下的POJO類。
StudentInfo.java
public class StudentInfo {
private Core[] core;
private Kaju[] kaju;
public Core[] getCore() {
return core;
}
public void setCore(Core[] core) {
this.core = core;
}
@Override
public String toString() {
return "ClassPojo [core = " + core + "][kaju = " + kaju + "]";
}
public Kaju[] getKaju() {
return kaju;
}
public void setKaju(Kaju[] kaju) {
this.kaju = kaju;
}
}
Core.java
public class Core {
private Id id;
private Entity entity;
private Sno Sno;
public Id getId() {
return id;
}
public void setId(Id id) {
this.id = id;
}
public Entity getEntity() {
return entity;
}
public void setEntity(Entity entity) {
this.entity = entity;
}
public Sno getSno() {
return Sno;
}
public void setSno(Sno Sno) {
this.Sno = Sno;
}
@Override
public String toString() {
return "ClassPojo [id = " + id.toString() + ", entity = " + entity.toString() + ", Sno = " + Sno.toString() + "]";
}
}
Id.java
public class Id {
private String value;
private String datatype;
private String type;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getDatatype() {
return datatype;
}
public void setDatatype(String datatype) {
this.datatype = datatype;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public String toString() {
return "ClassPojo [value = " + value + ", datatype = " + datatype + ", type = " + type + "]";
}
}
Kaju.java
public class Kaju {
private Sno Sno;
public Sno getSno() {
return Sno;
}
public void setSno(Sno Sno) {
this.Sno = Sno;
}
@Override
public String toString() {
return "ClassPojo [Sno = " + Sno + "]";
}
}
Entity.java
public class Entity {
private String value;
private String type;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public String toString() {
return "ClassPojo [value = " + value + ", type = " + type + "]";
}
}
Sno.java
public class Sno {
private String value;
private String type;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public String toString() {
return "ClassPojo [value = " + value + ", type = " + type + "]";
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.