如何从json数组中提取值

时间:2019-10-10 06:36:27

标签: java 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": {}
        }
    }
]

现在,我必须一一从响应中提取消息和决策,我该怎么做。 不能低于一个对象中可能有多个消息。

3 个答案:

答案 0 :(得分:2)

不确定load' 1: from C:/RailsInstaller_2_2_5/Ruby26/lib/ruby/2.6.0/json/common.rb:156:invalue的类型。有时是message,有时是xxx,xxx,xxx。所以您可能需要这个。

[xxx,xxx,xxx]

答案 1 :(得分:1)

尝试:

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();
}

答案 2 :(得分:0)

假设您有权访问jquery,并且需要使用独立数组中的消息来进行处理,

$(a).map(function (i, item) {return {item.message.value}).get();