由于某些原因,我必须在我的项目中使用特定的字符串。这是文本文件(它是一个JSON文件):
{"algorithm":
[
{ "key": "onGapLeft", "value" : "moveLeft" },
{ "key": "onGapFront", "value" : "moveForward" },
{ "key": "onGapRight", "value" : "moveRight" },
{ "key": "default", "value" : "moveBackward" }
]
}
我在JAVA中定义了这样:
static String input = "{\"algorithm\": \n"+
"[ \n" +
"{ \"key\": \"onGapLeft\", \"value\" : \"moveLeft\" }, \n" +
"{ \"key\": \"onGapFront\", \"value\" : \"moveForward\" }, \n" +
"{ \"key\": \"onGapRight\", \"value\" : \"moveRight\" }, \n" +
"{ \"key\": \"default\", \"value\" : \"moveBackward\" } \n" +
"] \n" +
"}";
现在我必须隔离数组中的键和值:
key[0] = onGapLeft; value[0] = moveLeft;
key[1] = onGapFront; value[1] = moveForward;
key[2] = onGapRight; value[2] = moveRight;
key[3] = default; value[3] = moveBackward;
我是JAVA的新手并且不太了解字符串类。有没有简单的方法来达到这个结果?你真的会帮助我!
谢谢!
更新: 对不起,我说得不够好。该程序将在LEGO NXT Robot上运行。 JSON不会像我想要的那样在那里工作所以我必须将这个JSON文件解释为普通的STRING!希望能解释我想要的东西:)
答案 0 :(得分:2)
我提出了几个步骤的解决方案。
1)让我们来看看~JSON字符串的不同部分。我们将使用模式来获取不同的{.*}
部分:
public static void main(String[] args) throws Exception{
List<String> lines = new ArrayList<String>();
Pattern p = Pattern.compile("\\{.*\\}");
Matcher matcher = p.matcher(input);
while (matcher.find()) {
lines.add(matcher.group());
}
}
现在,lines
包含4个字符串:
{ "key": "onGapLeft", "value" : "moveLeft" }
{ "key": "onGapFront", "value" : "moveForward" }
{ "key": "onGapRight", "value" : "moveRight" }
{ "key": "default", "value" : "moveBackward" }
给定一个类似于其中一个的字符串,您可以通过调用String#replaceAll();
List<String> cleanLines = new ArrayList<String>();
for(String line : lines) {
//replace curly brackets with... nothing.
//added a call to trim() in order to remove whitespace characters.
cleanLines.add(line.replaceAll("[{}]","").trim());
}
(你应该看一下String String#replaceAll(String regex))
现在,cleanLines
包含:
"key": "onGapLeft", "value" : "moveLeft"
"key": "onGapFront", "value" : "moveForward"
"key": "onGapRight", "value" : "moveRight"
"key": "default", "value" : "moveBackward"
2)让我们解析其中一行:
给出如下行:
"key": "onGapLeft", "value" : "moveLeft"
您可以使用String#split()在,
字符上拆分它。它会给你一个包含2个元素的String []:
//parts[0] = "key": "onGapLeft"
//parts[1] = "value" : "moveLeft"
String[] parts = line.split(",");
(你应该看一下String[] String#split(String regex))
让我们清理那些部分(删除“”)并将它们分配给一些变量:
String keyStr = parts[0].replaceAll("\"","").trim(); //Now, key = key: onGapLeft
String valueStr = parts[1].replaceAll("\"","").trim();//Now, value = value : moveLeft
//Then, you split `key: onGapLeft` with character `:`
String key = keyStr.split(":")[1].trim();
//And the same for `value : moveLeft` :
String value = valueStr.split(":")[1].trim();
就是这样!
你还应该看看Oracle's tutorial on regular expressions(这个非常重要,你应该花些时间)。
答案 1 :(得分:0)
您需要在此处使用JSON解析器库。例如,使用org.json,您可以将其解析为
String input = "{\"algorithm\": \n"+
"[ \n" +
"{ \"key\": \"onGapLeft\", \"value\" : \"moveLeft\" }, \n" +
"{ \"key\": \"onGapFront\", \"value\" : \"moveForward\" }, \n" +
"{ \"key\": \"onGapRight\", \"value\" : \"moveRight\" }, \n" +
"{ \"key\": \"default\", \"value\" : \"moveBackward\" } \n" +
"] \n" +
"}";
JSONObject root = new JSONObject(input);
JSONArray map = root.getJSONArray("algorithm");
for (int i = 0; i < map.length(); i++) {
JSONObject entry = map.getJSONObject(i);
System.out.println(entry.getString("key") + ": "
+ entry.getString("value"));
}
输出:
onGapLeft: moveLeft
onGapFront: moveForward
onGapRight: moveRight
default: moveBackward