我获取到一个JSONOBject,参数为"{"123":{"a":0,"b":1,"c":2},"456":{"a":0,"b":1,"c":2}}",我想获取到123和456里面的a、b和c值,请编写java代码,我事先不知道JSONOBject里面有什么数据
时间: 2024-06-05 14:04:35 浏览: 80
可以使用JSON库来处理JSONObject,以下是Java代码示例:
```
import org.json.JSONObject;
public class JSONExample {
public static void main(String[] args) {
String jsonString = "{\"123\":{\"a\":0,\"b\":1,\"c\":2},\"456\":{\"a\":0,\"b\":1,\"c\":2}}";
JSONObject jsonObject = new JSONObject(jsonString);
for (String key : jsonObject.keySet()) {
JSONObject subObject = jsonObject.getJSONObject(key);
System.out.println("Key: " + key);
System.out.println("a: " + subObject.getInt("a"));
System.out.println("b: " + subObject.getInt("b"));
System.out.println("c: " + subObject.getInt("c"));
}
}
}
```
输出结果为:
```
Key: 123
a: 0
b: 1
c: 2
Key: 456
a: 0
b: 1
c: 2
```
阅读全文