gpt4 book ai didi

java - 高效解析庞大的字符串响应

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:19:11 24 4
gpt4 key购买 nike

我有一项服务可以按以下格式返回数据。为了便于理解,我已将其缩短,但总的来说,这是相当大的回应。格式总是一样的。

process=true
version=2
DataCenter=dc2
Total:2
prime:{0=1, 1=2, 2=3, 3=4, 4=1, 5=2}
obvious:{0=6, 1=7, 2=8, 3=5, 4=6}
mapping:{3=machineA.dc2.com, 2=machineB.dc2.com}
Machine:[machineA.dc2.com, machineB.dc2.com]
DataCenter=dc1
Total:2
prime:{0=1, 1=2, 2=3, 3=4, 4=1, 5=2, 6=3}
obvious:{0=6, 1=7, 2=8, 3=5, 4=6, 5=7}
mapping:{3=machineP.dc1.com, 2=machineQ.dc1.com}
Machine:[machineP.dc1.com, machineQ.dc1.com]
DataCenter=dc3
Total:2
prime:{0=1, 1=2, 2=3, 3=4, 4=1, 5=2}
obvious:{0=6, 1=7, 2=8, 3=5, 4=6}
mapping:{3=machineO.dc3.com, 2=machineR.dc3.com}
Machine:[machineO.dc3.com, machineR.dc3.com]

我正在尝试解析上述数据并将其存储在三个不同的 Maps 中。

  • 主要 map :Map<String, Map<Integer, Integer>> prime = new HashMap<String, Map<Integer, Integer>>();
  • 明显 map :Map<String, Map<Integer, Integer>> obvious = new HashMap<String, Map<Integer, Integer>>();
  • 映射 map :Map<String, Map<Integer, String>> mapping = new HashMap<String, Map<Integer, String>>();

下面是描述:

  • 在 Prime map 中,键将是 dc2值将为 {0=1, 1=2, 2=3, 3=4, 4=1, 5=2} .
  • 在 Obvious 映射中,key 将是 dc2值将为 {0=6, 1=7, 2=8, 3=5, 4=6} .
  • 在 Mapping map 中,key 将为 dc2值将为 {3=machineA.dc2.com, 2=machineB.dc2.com} .

对于其他数据中心也是如此。

解析上述字符串响应的最佳方法是什么?我应该在这里使用正则表达式还是简单的字符串解析?

public class DataParser {
public static void main(String[] args) {
String response = getDataFromURL();
// here response will contain above string
parseResponse(response);
}

private void parseResponse(final String response) {
// what is the best way to parse the response?
}
}

任何示例都会有很大帮助。

最佳答案

您可以像 ShellFish 推荐的那样,将响应拆分为 '\n',然后处理每一行。

一种正则表达式方法如下(它不完整,但足以让您入门):

public static void main(String[] args) throws Exception {
String response = "process=true\n" +
"version=2\n" +
"DataCenter=dc2\n" +
" Total:2\n" +
" prime:{0=1, 1=2, 2=3, 3=4, 4=1, 5=2}\n" +
" obvious:{0=6, 1=7, 2=8, 3=5, 4=6}\n" +
" mapping:{3=machineA.dc2.com, 2=machineB.dc2.com}\n" +
" Machine:[machineA.dc2.com, machineB.dc2.com]\n" +
"DataCenter=dc1\n" +
" Total:2\n" +
" prime:{0=1, 1=2, 2=3, 3=4, 4=1, 5=2, 6=3}\n" +
" obvious:{0=6, 1=7, 2=8, 3=5, 4=6, 5=7}\n" +
" mapping:{3=machineP.dc1.com, 2=machineQ.dc1.com}\n" +
" Machine:[machineP.dc1.com, machineQ.dc1.com]\n" +
"DataCenter=dc3\n" +
" Total:2\n" +
" prime:{0=1, 1=2, 2=3, 3=4, 4=1, 5=2}\n" +
" obvious:{0=6, 1=7, 2=8, 3=5, 4=6}\n" +
" mapping:{3=machineO.dc3.com, 2=machineR.dc3.com}\n" +
" Machine:[machineO.dc3.com, machineR.dc3.com]";

Map<String, Map<Integer, Integer>> prime = new HashMap();
Map<String, Map<Integer, Integer>> obvious = new HashMap();
Map<String, Map<Integer, String>> mapping = new HashMap();

String outerMapKey = "";
int findCount = 0;
Matcher matcher = Pattern.compile("(?<=DataCenter=)(.*)|(?<=prime:)(.*)|(?<=obvious:)(.*)|(?<=mapping:)(.*)").matcher(response);
while(matcher.find()) {
switch (findCount) {
case 0:
outerMapKey = matcher.group();
break;
case 1:
prime.put(outerMapKey, new HashMap());
String group = matcher.group().replaceAll("[\\{\\}]", "").replaceAll(", ", ",");
String[] groupPieces = group.split(",");
for (String groupPiece : groupPieces) {
String[] keyValue = groupPiece.split("=");
prime.get(outerMapKey).put(Integer.parseInt(keyValue[0]), Integer.parseInt(keyValue[0]));
}
break;
// Add additional cases for obvious and mapping
}

findCount++;
if (findCount == 4) {
findCount = 0;
}
}

System.out.println("Primes:");
prime.keySet().stream().forEach(k -> System.out.printf("Key: %s Value: %s\n", k, prime.get(k)));
// Add additional outputs for obvious and mapping
}

结果:

Primes:
Key: dc2 Value: {0=0, 1=1, 2=2, 3=3, 4=4, 5=5}
Key: dc1 Value: {0=0, 1=1, 2=2, 3=3, 4=4, 5=5, 6=6}
Key: dc3 Value: {0=0, 1=1, 2=2, 3=3, 4=4, 5=5}

解释正则表达式模式的引用资料: http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html

http://www.regular-expressions.info/lookaround.html

关于java - 高效解析庞大的字符串响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30992488/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com