LEATH
尝试这个 :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();}
长风秋雁
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; }}