I'm very new to Java, I'm using it to teach my Lego NXT Robot some ways out of a labyrinth. The algorithm parameters shall be outsourced and loaded in the code, so thats why I use JSON. MY JSON file is pretty simple (lefthand algorthm):
我是Java的新手,我用它来教我的Lego NXT机器人以某种方式摆脱迷宫。算法参数应该外包并加载到代码中,这就是我使用JSON的原因。我的JSON文件很简单(左手algorthm):
{"algorithm":
{
"onGapLeft": "moveLeft",
"onGapFront": "moveForward",
"onGapRight": "moveRight",
"default": "moveBackward"
}
}
It's very important that this file is read in it's order. I.e. if you change Left and Right the algorithm would become a Right Hand Algorithm. This is the Java Code so far, I hope you understand what I'm trying to do. BTW: I'm using JSON.simple!
这个文件按顺序读取是非常重要的。即如果你改变左和右,算法将成为右手算法。到目前为止,这是Java代码,我希望你能理解我正在做的事情。顺便说一句:我正在使用JSON.simple!
private static void loadAlgorithm() throws InterruptedException {
JSONParser parser = new JSONParser();
Object obj = parser.parse(new FileReader("lefthand.json"));
JSONObject jsonObject = (JSONObject) obj;
JSONArray algorithm = (JSONArray) jsonObject.get("algorithm");
int length = algorithm.size();
for(int i = 0; i < length; i++)
{
switch (algorithm[i].key)
{
case "onGapLeft" : leftPos = i; break;
case "onGapFront": frontPos = i; break;
case "onGapRight": rightPos = i; break;
default: break;
}
switch (algorithm[i].value)
{
case "moveLeft" : directionAlgorithm[i] = direction.Left; break;
case "moveFront" : directionAlgorithm[i] = direction.Forward; break;
case "moveRight" : directionAlgorithm[i] = direction.Right; break;
case "moveBackward": directionAlgorithm[3] = direction.Backward; break;
default: break;
}
}
}
I'll need to know now wether it is possible to get the key string (where I used algorithm[i].key actually) and the same for the value string (algorithm[i].value).
我现在需要知道是否有可能获得密钥字符串(我实际上使用了算法[i] .key)和值字符串(algorithm [i] .value)相同。
Thank you very much for your help!
非常感谢您的帮助!
2 个解决方案
#1
2
You should probably change your JSON so that it is ordered, something like this:
你可能应该更改你的JSON以便它被订购,如下所示:
{"algorithm":
[
{ "key": "onGapLeft", "value" : "moveLeft" },
{ "key": "onGapFront", "value" : "moveForward" },
{ "key": "onGapRight", "value" : "moveRight" },
{ "key": "default", "value" : "moveBackward" }
]
}
And then modify your Java accordingly.
然后相应地修改Java。
#2
0
I am not familiar with JSON , but Since JSONObject is backed by a HashMap, you may be able to get the keys and values into a array in the same order like below,
我不熟悉JSON,但由于JSONObject由HashMap支持,您可以按照下面的相同顺序将键和值放入数组中,
Map<K, V> map = new HashMap<K, V>();
K[] keys = new K[map.size()];
V[] values = new V[map.size()];
int index = 0;
for (Map.Entry<K, V> mapEntry : map.entrySet()) {
keys[index] = mapEntry.getKey();
values[index] = mapEntry.getValue();
index++;
}
#1
2
You should probably change your JSON so that it is ordered, something like this:
你可能应该更改你的JSON以便它被订购,如下所示:
{"algorithm":
[
{ "key": "onGapLeft", "value" : "moveLeft" },
{ "key": "onGapFront", "value" : "moveForward" },
{ "key": "onGapRight", "value" : "moveRight" },
{ "key": "default", "value" : "moveBackward" }
]
}
And then modify your Java accordingly.
然后相应地修改Java。
#2
0
I am not familiar with JSON , but Since JSONObject is backed by a HashMap, you may be able to get the keys and values into a array in the same order like below,
我不熟悉JSON,但由于JSONObject由HashMap支持,您可以按照下面的相同顺序将键和值放入数组中,
Map<K, V> map = new HashMap<K, V>();
K[] keys = new K[map.size()];
V[] values = new V[map.size()];
int index = 0;
for (Map.Entry<K, V> mapEntry : map.entrySet()) {
keys[index] = mapEntry.getKey();
values[index] = mapEntry.getValue();
index++;
}