我有一个json字符串
{
"query": {
"filtered": {
"filter": {
"bool": {
"must": [{
"terms": {
"task_id": ["000JAGUAR8M,000JAGUAR94,000JAG0009I,000JQQ0009O,000JAGUAR6Y,000JAGUAR7H,000JAGUAR7I,000JAKAAL7J,000BAKAAL7K,000JAGUAR7L"]
}
}]
}
}
}
},
"size": 0,
"aggs": {
"task_ids": {
"terms": {
"field": "task_id",
"size": 0
}
}
}
}
我只需要替换task_id
部分内部术语,以便每个任务都在双引号内。
{
"query": {
"filtered": {
"filter": {
"bool": {
"must": [{
"terms": {
"task_id": ["000JAGUAR8M","000JAGUAR94","000JAG0009I","000JQQ0009O","000JAGUAR6Y","000JAGUAR7H","000JAGUAR7I","000JAKAAL7J","000BAKAAL7K","000JAGUAR7L"]
}
}]
}
}
}
},
"size": 0,
"aggs": {
"task_ids": {
"terms": {
"field": "task_id",
"size": 0
}
}
}
}
我可以使用下面的代码读取task_ids,但是如何在json对象中写回更改的值(requiredTasksIdValues
)?
String str = "{\"query\":{\"filtered\":{\"filter\":{\"bool\":{\"must\":[{\"terms\":{\"task_id\":[\"000JAGUAR8M,000JAGUAR94,000JAG0009I,000JQQ0009O,000JAGUAR6Y,000JAGUAR7H,000JAGUAR7I,000JAKAAL7J,000BAKAAL7K,000JAGUAR7L\"]}}]}}}},\"size\":0,\"aggs\":{\"task_ids\":{\"terms\":{\"field\":\"task_id\",\"size\":0}}}}";
ObjectMapper mapper = new ObjectMapper();
try {
JsonNode taskIdsjsonNode = mapper.readTree(str);
JsonNode jn = taskIdsjsonNode.get("query").get("filtered").get("filter").get("bool").get("must").get(0).get("terms").get("task_id");
StringBuffer temp = new StringBuffer(jn.toString().substring(2));
temp = new StringBuffer(temp.substring(0, temp.length() - 2));
String[] temp_task_ids = temp.toString().split(",");
StringBuffer requiredTasksIdValues = new StringBuffer();
for (String taskid: temp_task_ids) {
requiredTasksIdValues.append("\""+taskid+"\",");
}
requiredTasksIdValues = new StringBuffer(requiredTasksIdValues.substring(0, requiredTasksIdValues.length() - 1));
System.out.println(requiredTasksIdValues);
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(str);
答案 0 :(得分:1)
String original = "000JAGUAR8M,000JAGUAR94,000JAG0009I,000JQQ0009O,000JAGUAR6Y,000JAGUAR7H,000JAGUAR7I,000JAKAAL7J,000BAKAAL7K,000JAGUAR7L";
String[] elements = original.split(",");
System.out.println(Arrays.toString(elements));