如何从 json 数组中提取值

我正在调用一个 Api,它返回给我这个响应


[

    {

        "message": {

            "type": "String",

            "value": "Msg1,Msg2,Msg3,Msg4",

            "valueInfo": {}

        },

        "Decision": {

            "type": "Boolean",

            "value": false,

            "valueInfo": {}

        }

    },

    {

        "message": {

            "type": "String",

            "value": "[Msg2, Msg3,Msg5]",

            "valueInfo": {}

        },

        "Decision": {

            "type": "Boolean",

            "value": false,

            "valueInfo": {}

        }

    }

]

现在我必须从响应中一一提取消息和决策我该如何做到这一点。不向下,一个对象中可能有多个消息。


翻阅古今
浏览 101回答 3
3回答

LEATH

尝试这个 :String res = "[\n" +&nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; {\n" +&nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; \"message\": {\n" +&nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"type\": \"String\",\n" +&nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"value\": \"Msg1,Msg2,Msg3,Msg4\",\n" +&nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"valueInfo\": {}\n" +&nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; },\n" +&nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; \"Decision\": {\n" +&nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"type\": \"Boolean\",\n" +&nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"value\": false,\n" +&nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"valueInfo\": {}\n" +&nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; }\n" +&nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; },\n" +&nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; {\n" +&nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; \"message\": {\n" +&nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"type\": \"String\",\n" +&nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"value\": \"[Msg2, Msg3,Msg5]\",\n" +&nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"valueInfo\": {}\n" +&nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; },\n" +&nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; \"Decision\": {\n" +&nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"type\": \"Boolean\",\n" +&nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"value\": false,\n" +&nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"valueInfo\": {}\n" +&nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; }\n" +&nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; }\n" +&nbsp; &nbsp; &nbsp; &nbsp; "]";try {&nbsp; &nbsp; JSONArray jsonArray = new JSONArray(res);&nbsp; &nbsp; JSONObject[] messages = new JSONObject[jsonArray.length()];&nbsp; &nbsp; JSONObject[] Decision = new JSONObject[jsonArray.length()];&nbsp; &nbsp; for(int i =0; i<jsonArray.length(); i++){&nbsp; &nbsp; &nbsp; &nbsp; messages[i] = ((JSONObject) (jsonArray.get(i))).getJSONObject("message");&nbsp; &nbsp; &nbsp; &nbsp; Decision[i] = ((JSONObject) (jsonArray.get(i))).getJSONObject("Decision");&nbsp; &nbsp; }} catch (JSONException e) {&nbsp; &nbsp; e.printStackTrace();}

守候你守候我

假设您可以访问 jquery 并需要使用独立数组中的消息,$(a).map(function (i, item) {return {item.message.value}).get();

长风秋雁

valuein的类型message不确定。有时是xxx,xxx,xxx,有时是[xxx,xxx,xxx]。所以你可能需要这个。public static void main(String[] args) {&nbsp; &nbsp; String json = "[\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; {\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; \"message\": {\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"type\": \"String\",\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"value\": \"Msg1,Msg2,Msg3,Msg4\",\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"valueInfo\": {}\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; },\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; \"Decision\": {\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"type\": \"Boolean\",\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"value\": false,\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"valueInfo\": {}\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; }\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; },\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; {\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; \"message\": {\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"type\": \"String\",\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"value\": \"[Msg2, Msg3,Msg5]\",\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"valueInfo\": {}\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; },\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; \"Decision\": {\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"type\": \"Boolean\",\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"value\": false,\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"valueInfo\": {}\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; }\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; }\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "]";&nbsp; &nbsp; List<MessageDecision> list = new Gson().fromJson(json,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new TypeToken<List<MessageDecision>>(){}.getType());&nbsp; &nbsp; for (MessageDecision data: list){&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(data.message.getValueList());&nbsp; &nbsp; }}class MessageDecision {&nbsp; &nbsp; public Message message;&nbsp; &nbsp; public Decision Decision;&nbsp; &nbsp; class Message {&nbsp; &nbsp; &nbsp; &nbsp; String type;&nbsp; &nbsp; &nbsp; &nbsp; String value;&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; &nbsp; List<String> valueList;&nbsp; &nbsp; &nbsp; &nbsp; public List<String> getValueList() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (type.equals("String")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (value!=null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (value.startsWith("[")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; valueList = new Gson().fromJson(value, new TypeToken<List<String>>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }.getType());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String tempValues[] = value.split(",");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; valueList = Arrays.asList(tempValues);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return valueList;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; class Decision {&nbsp; &nbsp; &nbsp; &nbsp; String type;&nbsp; &nbsp; &nbsp; &nbsp; Object value;&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java