0

由于某些原因,我必须在我的项目中使用特定的字符串。这是文本文件(它是一个 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 机器人上运行。JSON 不会像我想要的那样在那里工作,所以我必须将此 JSON 文件解释为普通字符串!希望能解释我想要什么:)

4

2 回答 2

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());
  }
}

(你应该看看PatternMatcher

现在,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 的正则表达式教程(这个非常重要,你应该花时间在上面)。

于 2013-09-09T17:32:02.680 回答
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
于 2013-09-09T16:56:27.523 回答