我在这里非常需要帮助,我尝试从文件中解析json。我从kibana开发工具中获得了这个json内容,我计划使用弹性api,获取json结果,并从json中获取多个字段。这是我的代码
public class JSONParserTest {
public static void main(String[] args) throws IOException, JSONException {
String the_json=readFile("/Users/ahmadasyary/Documents/sample.out", StandardCharsets.UTF_8);
//System.out.println(the_json);
JSONObject myjson = new JSONObject(the_json);
JSONArray the_json_array = myjson.getJSONArray("profiles");
}
static String readFile(String path, Charset encoding)
throws IOException
{
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}
}
当我尝试运行它时,出现了这样的错误
Exception in thread "main" org.json.JSONException: Expected a ',' or '}' at 3264 [character 25 line 64]
at java.json/org.json.JSONTokener.syntaxError(JSONTokener.java:432)
at java.json/org.json.JSONObject.<init>(JSONObject.java:223)
at java.json/org.json.JSONTokener.nextValue(JSONTokener.java:362)
at java.json/org.json.JSONObject.<init>(JSONObject.java:208)
at java.json/org.json.JSONTokener.nextValue(JSONTokener.java:362)
at java.json/org.json.JSONArray.<init>(JSONArray.java:117)
at java.json/org.json.JSONTokener.nextValue(JSONTokener.java:365)
at java.json/org.json.JSONObject.<init>(JSONObject.java:208)
at java.json/org.json.JSONTokener.nextValue(JSONTokener.java:362)
at java.json/org.json.JSONObject.<init>(JSONObject.java:208)
at java.json/org.json.JSONObject.<init>(JSONObject.java:310)
at TDRParser/ParserTest.JSONParserTest.main(JSONParserTest.java:21)
这是来自kibana开发人员工具的json内容
{
"took": 89,
"timed_out": false,
"_shards": {
"total": 1015,
"successful": 1015,
"skipped": 970,
"failed": 0
},
"hits": {
"total": 1,
"max_score": null,
"hits": [
{
"_index": "project.prod-esb.cbd0dea7-4153-11e8-86d7-001a4a160175.2019.03.22",
"_type": "com.redhat.viaq.common",
"_id": "1KKHo2kB-rz4ABZ2mpyp",
"_version": 1,
"_score": null,
"_source": {
"logTimestamp": "2019-03-22T10:52:46.251+07:00",
"transactionID": "H003190322105245739910520",
"logLevel": "AUDIT",
"logPoint": "Transaction-Audit",
"logMessage": "2019-03-22T10:52:45|2019-03-22T10:52:46|492|p0|H003190322105245739910520|p0169a3879784910520001101905fe5Sac000000000|TELKOMSEL||ACT|SELF|AIRTIME|0|||6281360791052|prepaid|Simpati|||prepaid|Simpati|22016||00011019|ML4_BP_22184|PO||Campaign|2019030761549|1|Days|5000.0|OT_002758_00011019_a5ffc884-19b1-11e9-80a8-cfff756b35c7|||Success|00000|Success||||002777:TC_002775_FLEXIBLEOFFER;00005679:TC_00005457_FLEXIBLEOFFER;00005696:TC_00005474_FLEXIBLEOFFER",
"processTime": "492",
"docker": {
"container_id": "2a8e197e4bc9d849291e5457fdc7a238d684f92ccc19cf5f51b88772169acf0d"
},
"kubernetes": {
"container_name": "ordersubmission-bs-v2",
"namespace_name": "prod-esb",
"pod_name": "ordersubmission-bs-v2-42-6n2rn",
"pod_id": "5f417ed0-4b0d-11e9-89a5-001a4a160178",
"labels": {
"app": "ordersubmission-bs-v2",
"deployment": "ordersubmission-bs-v2-42",
"deploymentconfig": "ordersubmission-bs-v2"
},
"host": "esbwnpapp3.telkomsel.co.id",
"master_url": "https://kubernetes.default.svc.cluster.local",
"namespace_id": "cbd0dea7-4153-11e8-86d7-001a4a160175"
},
"systemd": {
"t": {
"BOOT_ID": "ea8d4663351b44f9888b93042f680fcd",
"CAP_EFFECTIVE": "1fffffffff",
"CMDLINE": "/usr/bin/dockerd-current --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current --default-runtime=docker-runc --authorization-plugin=rhel-push-plugin --exec-opt native.cgroupdriver=systemd --userland-proxy-path=/usr/libexec/docker/docker-proxy-current --log-driver=journald --storage-driver devicemapper --storage-opt dm.fs=xfs --storage-opt dm.thinpooldev=/dev/mapper/rhel-docker--pool --storage-opt dm.use_deferred_removal=true --storage-opt dm.use_deferred_deletion=true --mtu=1450 --add-registry registry.access.redhat.com --add-registry registry.access.redhat.com",
"COMM": "dockerd-current",
"EXE": "/usr/bin/dockerd-current",
"GID": "0",
"MACHINE_ID": "35ae721c7f2f4d2e9a0676e0d9e17589",
"PID": "2722",
"SELINUX_CONTEXT": "system_u:system_r:container_runtime_t:s0",
"SYSTEMD_CGROUP": "/system.slice/docker.service",
"SYSTEMD_SLICE": "system.slice",
"SYSTEMD_UNIT": "docker.service",
"TRANSPORT": "journal",
"UID": "0"
}
},
"level": "info",
"message": """{"logMessageId":"","logTimestamp":"2019-03-22T10:52:46.251+07:00","transactionID":"H003190322105245739910520","serviceName":"","applicationID":"","logLevel":"AUDIT","logPoint":"Transaction-Audit","logMessage":"2019-03-22T10:52:45|2019-03-22T10:52:46|492|p0|H003190322105245739910520|p0169a3879784910520001101905fe5Sac000000000|TELKOMSEL||ACT|SELF|AIRTIME|0|||6281360791052|prepaid|Simpati|||prepaid|Simpati|22016||00011019|ML4_BP_22184|PO||Campaign|2019030761549|1|Days|5000.0|OT_002758_00011019_a5ffc884-19b1-11e9-80a8-cfff756b35c7|||Success|00000|Success||||002777:TC_002775_FLEXIBLEOFFER;00005679:TC_00005457_FLEXIBLEOFFER;00005696:TC_00005474_FLEXIBLEOFFER","requestPayload":"","responsePayload":"","processTime":"492"}""",
"hostname": "esbwnpapp3.telkomsel.co.id",
"pipeline_metadata": {
"collector": {
"ipaddr4": "10.130.14.2",
"ipaddr6": "fe80::54d0:26ff:fe59:e4cd",
"inputname": "fluent-plugin-systemd",
"name": "fluentd",
"received_at": "2019-03-22T03:52:46.883461+00:00",
"version": "0.12.39 1.6.0"
}
},
"@timestamp": "2019-03-22T03:52:46.252005+00:00"
},
"fields": {
"logTimestamp": [
"2019-03-22T03:52:46.251Z"
],
"@timestamp": [
"2019-03-22T03:52:46.252Z"
],
"pipeline_metadata.collector.received_at": [
"2019-03-22T03:52:46.883Z"
]
},
"highlight": {
"logLevel.keyword": [
"@kibana-highlighted-field@AUDIT@/kibana-highlighted-field@"
],
"transactionID": [
"@kibana-highlighted-field@H003190322105245739910520@/kibana-highlighted-field@"
],
"kubernetes.container_name.keyword": [
"@kibana-highlighted-field@ordersubmission-bs-v2@/kibana-highlighted-field@"
]
},
"sort": [
1553226766252
]
}
]
},
"aggregations": {
"2": {
"buckets": [
{
"key_as_string": "2019-03-22T10:52:46.000+07:00",
"key": 1553226766000,
"doc_count": 1
}
]
}
}
}
我希望这个问题可以在无需对json内容进行任何手动更改的情况下解决,因为它是由kibana生成的
答案 0 :(得分:2)
只需完全删除两次出现在关键字“ message”中的双引号:
"level": "info", "message": {"logMessageId":"","logTimestamp":"2019-03-22T10:52:46.251+07:00","transactionID":"H003190322105245739910520","serviceName":"","applicationID":"","logLevel":"AUDIT","logPoint":"Transaction-Audit","logMessage":"2019-03-22T10:52:45|2019-03-22T10:52:46|492|p0|H003190322105245739910520|p0169a3879784910520001101905fe5Sac000000000|TELKOMSEL||ACT|SELF|AIRTIME|0|||6281360791052|prepaid|Simpati|||prepaid|Simpati|22016||00011019|ML4_BP_22184|PO||Campaign|2019030761549|1|Days|5000.0|OT_002758_00011019_a5ffc884-19b1-11e9-80a8-cfff756b35c7|||Success|00000|Success||||002777:TC_002775_FLEXIBLEOFFER;00005679:TC_00005457_FLEXIBLEOFFER;00005696:TC_00005474_FLEXIBLEOFFER","requestPayload":"","responsePayload":"","processTime":"492"}, "hostname": "esbwnpapp3.telkomsel.co.id",
然后它应该可以工作,我可以使用下面的代码片段来解析文件。
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class JSONParserTest {
public static void main(String[] args) throws ParseException, IOException {
String the_json=readFile("sample.out", StandardCharsets.UTF_8);
//System.out.println(the_json);
JSONParser object = new JSONParser();
Object obj = object.parse(the_json);
JSONObject myjson = (JSONObject)obj;
JSONArray the_json_array = (JSONArray)myjson.get("profiles");
System.out.println(the_json_array);
}
static String readFile(String path, Charset encoding)
throws IOException
{
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}
}