这是我的json数组。
[
{
"type":"select",
"label":"Select",
"className":"form-control",
"name":"select-1497355331262",
"values":[
{
"label":"Option 1",
"value":"option-1",
"selected":true
},
{
"label":"Option 2",
"value":"option-2"
},
{
"label":"Option 3",
"value":"option-3"
}
]
}
]
我的Appraoch:
JSONArray js = null;
try {
js = new JSONArray(json);
} catch (JSONException e) {
e.printStackTrace();
}
JSONObject obj = null;
for (int i = 0; i < js.length(); i++) {
try {
obj = js.getJSONObject(i);
} catch (JSONException e) {
e.printStackTrace();
}
try {
type = obj.getString("type");
} catch (JSONException e) {
type = null;
}
System.out.println("Type : " + type);
try {
JSONArray jContent = obj.getJSONArray("values");
System.out.println(jContent.toString());
for (int iterate = 0; iterate < jContent.length(); iterate++) {
JSONObject inner = jContent.getJSONObject(iterate);
String inner_label = (String) inner.get("label");
System.out.println(inner_label);
String val = (String) inner.get("value");
boolean sel;
try {
sel = (boolean) inner.get("selected");
} catch (JSONException j) {
sel = false;
}
System.out.print(inner_label);
System.out.print(" " + val);
System.out.println(" " + sel);
}
} catch (JSONException e) {
System.out.println("There is some error");
}
try {
label = obj.getString("label");
} catch (JSONException e) {
e.printStackTrace();
}
try {
access = obj.getBoolean("access");
} catch (JSONException e) {
access = false;
}
try {
role = obj.getString("role");
} catch (JSONException e) {
role = null;
}
try {
subtype = obj.getString("subtype");
} catch (JSONException e) {
subtype = null;
}
try {
maxlength = obj.getString("maxlength");
} catch (JSONException e) {
maxlength = null;
}
try {
name = obj.getString("name");
} catch (JSONException e) {
name = null;
}
try {
description = obj.getString("description");
} catch (JSONException e) {
description = null;
}
try {
classname = obj.getString("className");
} catch (JSONException e) {
classname = null;
}
try {
placeholder = obj.getString("placeholder");
} catch (JSONException e) {
placeholder = null;
}
try {
value = obj.getString("value");
} catch (JSONException e) {
value = null;
}
try {
required = obj.getBoolean("required");
} catch (JSONException e) {
required = false;
}
try {
toggle = obj.getBoolean("toggle");
} catch (JSONException e) {
toggle = false;
}
try {
inline = obj.getBoolean("inline");
} catch (JSONException e) {
inline = false;
}
try {
enableother = obj.getBoolean("other");
} catch (JSONException e) {
enableother = false;
}
try {
multipleFiles = obj.getBoolean("multiple");
} catch (JSONException e) {
multipleFiles = false;
}
try {
style = obj.getString("style");
} catch (JSONException e) {
style = null;
}
try {
min = obj.getString("min");
} catch (JSONException e) {
min = null;
}
try {
max = obj.getString("max");
} catch (JSONException e) {
max = null;
}
try {
step = obj.getString("step");
} catch (JSONException e) {
step = null;
}
try {
rows = obj.getString("rows");
} catch (JSONException e) {
rows = null;
}
我能够获取所有其他对象值,我无法读取values数组。我已经为它编写了代码,但它直接转到catch块打印错误消息&#34;有一些错误&#34;。向你求助。
答案 0 :(得分:0)
解决了...值不是一个数组它是一个对象....所以这就是我所做的。它奏效了。
try {
String j2 = obj.getString("values");
JSONArray jContent = new JSONArray(j2);
for (int iterate = 0; iterate < jContent.length(); iterate++) {
JSONObject inner = jContent.getJSONObject(iterate);
String inner_label = (String) inner.get("label");
System.out.println(inner_label);
String val = (String) inner.get("value");
boolean sel;
try {
sel = (boolean) inner.get("selected");
} catch (JSONException j) {
sel = false;
}
System.out.print(inner_label);
System.out.print(" " + val);
System.out.println(" " + sel);
}