在 Java 中获取 JSON 个嵌套数组元素
Get JSON Nested Array Element in Java
我正在使用 org.json
查找 json 对象和值(org.json
是一项要求),并且我正在尝试访问子数组元素。
我的json:
{
"Info": {
"name": "my_json",
},
"my_array": {
"arrays": [
{
"array 1": [
{
"name": "red",
"server": "red1",
"capacity": "123"
},
{
"name": "blue",
"server": "blue1",
"capacity": "456"
}
]
},
{
"array 2": [
{
"name": "white",
"server": "white1",
"capacity": "1234"
},
{
"name": "black",
"server": "black1",
"capacity": "4567"
}
]
}
]
}
}
这输出:
{"array 1":[
{"name":"red","capacity":"123","server":"red1"},
{"capacity":"456","name":"blue","name":"blue1"}
]}
{"array 2":[
{"capacacity":"1234","name":"white","server":"white1"},
{"name":"black","capacity":"4567","server":"black1"}
]}
{"array 1":[
{"name":"red","capacity":"123","server":"red1"},
{"capacity":"456","name":"blue","name":"blue1"}
]}
{"array 2":[
{"capacity":"1234","name":"white","server":"white1"},
{"name":"black","capacity":"4567","server":"black1"}
]}
该方法如下所示:
public static String processJson(String[] args) throws JSONException {
String value = "";
String jsonData = readFile(args[0]);
JSONObject jobj = new JSONObject(jsonData);
if (args[1].equals("my_array")) {
JSONObject parent = jobj.getJSONObject("my_array");
JSONArray jarr = parent.getJSONArray("arrays");
for (int i = 0; i < jarr.length(); i++) {
for (int j = 0; j < jarr.length(); j++) {
JSONObject test1 = jarr.getJSONObject(j);
System.out.println(test1);
}
}
}
return value;
}
我希望 return 值为:
[{"name":"red","capacity":"123","server":"red1"
{"capacity":"456","name":"blue","name":"blue1"}]
是否可以获得array 1
个元素?
我以为嵌套循环会处理它,但它只输出相同的时间。
如果您只需要第一个元素,则不需要循环
JSONObject test1 = jarr.getJSONObject(0);
System.out.println(test1);
如果你想格式化test1
你可以
System.out.println (test1.toString ().replace ("{\"array 1\":", ""));
我正在使用 org.json
查找 json 对象和值(org.json
是一项要求),并且我正在尝试访问子数组元素。
我的json:
{
"Info": {
"name": "my_json",
},
"my_array": {
"arrays": [
{
"array 1": [
{
"name": "red",
"server": "red1",
"capacity": "123"
},
{
"name": "blue",
"server": "blue1",
"capacity": "456"
}
]
},
{
"array 2": [
{
"name": "white",
"server": "white1",
"capacity": "1234"
},
{
"name": "black",
"server": "black1",
"capacity": "4567"
}
]
}
]
}
}
这输出:
{"array 1":[
{"name":"red","capacity":"123","server":"red1"},
{"capacity":"456","name":"blue","name":"blue1"}
]}
{"array 2":[
{"capacacity":"1234","name":"white","server":"white1"},
{"name":"black","capacity":"4567","server":"black1"}
]}
{"array 1":[
{"name":"red","capacity":"123","server":"red1"},
{"capacity":"456","name":"blue","name":"blue1"}
]}
{"array 2":[
{"capacity":"1234","name":"white","server":"white1"},
{"name":"black","capacity":"4567","server":"black1"}
]}
该方法如下所示:
public static String processJson(String[] args) throws JSONException {
String value = "";
String jsonData = readFile(args[0]);
JSONObject jobj = new JSONObject(jsonData);
if (args[1].equals("my_array")) {
JSONObject parent = jobj.getJSONObject("my_array");
JSONArray jarr = parent.getJSONArray("arrays");
for (int i = 0; i < jarr.length(); i++) {
for (int j = 0; j < jarr.length(); j++) {
JSONObject test1 = jarr.getJSONObject(j);
System.out.println(test1);
}
}
}
return value;
}
我希望 return 值为:
[{"name":"red","capacity":"123","server":"red1"
{"capacity":"456","name":"blue","name":"blue1"}]
是否可以获得array 1
个元素?
我以为嵌套循环会处理它,但它只输出相同的时间。
如果您只需要第一个元素,则不需要循环
JSONObject test1 = jarr.getJSONObject(0);
System.out.println(test1);
如果你想格式化test1
你可以
System.out.println (test1.toString ().replace ("{\"array 1\":", ""));