Map<String,List>取值问题

"data": [
    {
        "playTime": "2022-05-13T08:13:27.000+00:00",
        "playerIng": [
            {
                "id": 148,
                "playTime": "2022-05-13T08:13:27.000+00:00",
                "playerId": 111,
                "holeIndex": 1,
                "position": "1",
                "direction": "111",
                "par": 1,
                "kickYard": 11.0,
                "yard": 11.0,
                "sort": "1"
            },
            {
                "id": 148,
                "playTime": "2022-05-13T08:13:27.000+00:00",
                "playerId": 11,
                "holeIndex": 1,
                "position": "5",
                "direction": "11",
                "par": 1,
                "kickYard": 10.0,
                "yard": 10.0,
                "sort": "2"
            }
        ]
    }
        ]

有这种数据格式,我怎么才能取出playTime对应的value值
直接使用map.get("playerTime"),值的类型是List<实体类>

阅读 1.7k
1 个回答

不知是否是你想要的。

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * @author lpe234
 */
public class JsonParseDemo {

    public static void main(String[] args) throws JSONException {
        String xxx = "{\n" +
                "  \"data\": [\n" +
                "    {\n" +
                "      \"playTime\": \"2022-05-13T08:13:27.000+00:00\",\n" +
                "      \"playerIng\": [\n" +
                "        {\n" +
                "          \"id\": 148,\n" +
                "          \"playTime\": \"2022-05-13T08:13:27.000+00:00\",\n" +
                "          \"playerId\": 111,\n" +
                "          \"holeIndex\": 1,\n" +
                "          \"position\": \"1\",\n" +
                "          \"direction\": \"111\",\n" +
                "          \"par\": 1,\n" +
                "          \"kickYard\": 11.0,\n" +
                "          \"yard\": 11.0,\n" +
                "          \"sort\": \"1\"\n" +
                "        },\n" +
                "        {\n" +
                "          \"id\": 148,\n" +
                "          \"playTime\": \"2022-05-13T08:13:27.000+00:00\",\n" +
                "          \"playerId\": 11,\n" +
                "          \"holeIndex\": 1,\n" +
                "          \"position\": \"5\",\n" +
                "          \"direction\": \"11\",\n" +
                "          \"par\": 1,\n" +
                "          \"kickYard\": 10.0,\n" +
                "          \"yard\": 10.0,\n" +
                "          \"sort\": \"2\"\n" +
                "        }\n" +
                "      ]\n" +
                "    }\n" +
                "  ]\n" +
                "}";
        JSONObject jobj = new JSONObject(xxx);
        JSONArray jarr = jobj.getJSONArray("data");
        for (int i = 0; i < jarr.length(); i++) {
            JSONObject jobj2 = jarr.getJSONObject(i);
            String playTime = jobj2.getString("playTime");
            System.out.println("[data.playTime] " + playTime);
            // 获取data.playerIng中的playTime
            JSONArray playerIngArr = jobj2.getJSONArray("playerIng");
            for (int j = 0; j < playerIngArr.length(); j++) {
                JSONObject jobj3 = playerIngArr.getJSONObject(j);
                String playTime2 = jobj3.getString("playTime");
                System.out.println("\t[data.playerIng(" + j + ").playTime] " + playTime2);
            }
        }
    }
}

结果:

[data.playTime] 2022-05-13T08:13:27.000+00:00
    [data.playerIng(0).playTime] 2022-05-13T08:13:27.000+00:00
    [data.playerIng(1).playTime] 2022-05-13T08:13:27.000+00:00

已参与了 SegmentFault 思否社区 10 周年「问答」打卡 ,欢迎正在阅读的你也加入。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题