3 回答
TA贡献1936条经验 获得超6个赞
尝试这个 :
String res = "[\n" +
" {\n" +
" \"message\": {\n" +
" \"type\": \"String\",\n" +
" \"value\": \"Msg1,Msg2,Msg3,Msg4\",\n" +
" \"valueInfo\": {}\n" +
" },\n" +
" \"Decision\": {\n" +
" \"type\": \"Boolean\",\n" +
" \"value\": false,\n" +
" \"valueInfo\": {}\n" +
" }\n" +
" },\n" +
" {\n" +
" \"message\": {\n" +
" \"type\": \"String\",\n" +
" \"value\": \"[Msg2, Msg3,Msg5]\",\n" +
" \"valueInfo\": {}\n" +
" },\n" +
" \"Decision\": {\n" +
" \"type\": \"Boolean\",\n" +
" \"value\": false,\n" +
" \"valueInfo\": {}\n" +
" }\n" +
" }\n" +
"]";
try {
JSONArray jsonArray = new JSONArray(res);
JSONObject[] messages = new JSONObject[jsonArray.length()];
JSONObject[] Decision = new JSONObject[jsonArray.length()];
for(int i =0; i<jsonArray.length(); i++){
messages[i] = ((JSONObject) (jsonArray.get(i))).getJSONObject("message");
Decision[i] = ((JSONObject) (jsonArray.get(i))).getJSONObject("Decision");
}
} catch (JSONException e) {
e.printStackTrace();
}
TA贡献1802条经验 获得超10个赞
假设您可以访问 jquery 并需要使用独立数组中的消息,
$(a).map(function (i, item) {return {item.message.value}).get();
TA贡献1757条经验 获得超7个赞
valuein的类型message不确定。有时是xxx,xxx,xxx,有时是[xxx,xxx,xxx]。所以你可能需要这个。
public static void main(String[] args) {
String json = "[\n" +
" {\n" +
" \"message\": {\n" +
" \"type\": \"String\",\n" +
" \"value\": \"Msg1,Msg2,Msg3,Msg4\",\n" +
" \"valueInfo\": {}\n" +
" },\n" +
" \"Decision\": {\n" +
" \"type\": \"Boolean\",\n" +
" \"value\": false,\n" +
" \"valueInfo\": {}\n" +
" }\n" +
" },\n" +
" {\n" +
" \"message\": {\n" +
" \"type\": \"String\",\n" +
" \"value\": \"[Msg2, Msg3,Msg5]\",\n" +
" \"valueInfo\": {}\n" +
" },\n" +
" \"Decision\": {\n" +
" \"type\": \"Boolean\",\n" +
" \"value\": false,\n" +
" \"valueInfo\": {}\n" +
" }\n" +
" }\n" +
"]";
List<MessageDecision> list = new Gson().fromJson(json,
new TypeToken<List<MessageDecision>>(){}.getType());
for (MessageDecision data: list){
System.out.println(data.message.getValueList());
}
}
class MessageDecision {
public Message message;
public Decision Decision;
class Message {
String type;
String value;
//
List<String> valueList;
public List<String> getValueList() {
if (type.equals("String")) {
if (value!=null) {
if (value.startsWith("[")) {
valueList = new Gson().fromJson(value, new TypeToken<List<String>>() {
}.getType());
} else {
String tempValues[] = value.split(",");
valueList = Arrays.asList(tempValues);
}
}
return valueList;
}
return null;
}
}
class Decision {
String type;
Object value;
}
}
添加回答
举报