前台传来json串,其中有数组,怎么去拿到这个数组

图片说明

后台是用request接收的

图片说明

我的思路是用string接收这个字符串,再用json解析,转成list

但是不知道怎么实现

String ryxxs = request.getParameter("ruxxs");

还有就是这句话能接收到传来的数组字符串吗

可以让前端传json串给你,你获取到字符串以后直接

List list = JSON.parseObject(str,ArrayList.class);

这样转一下就获取到了一个列表

先是通过解析原始json数据串,拿到集合的json串,再通过json工具直接转成集合对象即可。

可以使用google的gson工具类转换一下

package com.shms.controller.appinterface;

import org.json.*;

import com.shms.util.common.StringUtils;

public class TestJson {
    public static void main(String[] args) {
        test22();

    }

    //对象数组
    public static void test1() {
        String json = "{\"date\":{\"ryxxs\":[{\"rylx\":\"人员类型1\",\"xm\":\"姓名1\",\"xb\":\"性别1\"},{\"rylx\":\"人员类型2\",\"xm\":\"姓名2\",\"xb\":\"性别2\"},{\"rylx\":\"人员类型3\",\"xm\":\"姓名3\",\"xb\":\"性别3\"}]}}";
        JSONObject jsonObject = new JSONObject(json);
        JSONObject data = jsonObject.getJSONObject("date");
        JSONArray items = data.getJSONArray("ryxxs");
        JSONObject  row = null;
        for (int i = 0; i < items.length(); i++) {
            row = items.getJSONObject(i);
            System.out.println("人员类型:"+row.get("rylx"));
            System.out.println("姓名:"+row.get("xm"));
            System.out.println("性别:"+row.get("xb"));
        }
    }

    public static void test11() {
        String json = "[{\"rylx\":\"人员类型1\",\"xm\":\"姓名1\",\"xb\":\"性别1\"},{\"rylx\":\"人员类型2\",\"xm\":\"姓名2\",\"xb\":\"性别2\"},{\"rylx\":\"人员类型3\",\"xm\":\"姓名3\",\"xb\":\"性别3\"}]";
        JSONArray items = new JSONArray(json);
        JSONObject  row = null;
        for (int i = 0; i < items.length(); i++) {
            row = items.getJSONObject(i);
            System.out.println("人员类型:"+row.get("rylx"));
            System.out.println("姓名:"+row.get("xm"));
            System.out.println("性别:"+row.get("xb"));
        }

    }

   //属性与对象数组
    public static void test2() {
        String json = "{\"date\":{\"zjcls\":[{\"wjlx\":\"类型1\",\"wjids\":[{\"wjid\":\"id11\"},{\"wjid\":\"id12\"},{\"wjid\":\"id13\"}]},{\"wjlx\":\"类型2\",\"wjids\":[{\"wjid\":\"id21\"},{\"wjid\":\"id22\"},{\"wjid\":\"id23\"}]}]}}";
        JSONObject jsonObject = new JSONObject(json);
        JSONObject data = jsonObject.getJSONObject("date");
        JSONArray items = data.getJSONArray("zjcls");
        JSONObject  row = null;
        JSONArray  rowwjids = null;
        JSONObject  rowwjid = null;
        for (int i = 0; i < items.length(); i++) {
            row = items.getJSONObject(i);
            System.out.println("人员类型:"+row.get("wjlx"));
            rowwjids = row.getJSONArray("wjids");
            for (int j = 0; j < rowwjids.length(); j++) {
                rowwjid = rowwjids.getJSONObject(j);
                System.out.println(rowwjid.get("wjid"));
            }
            }
        }

    public static void test22() {
        String json = "[{\"wjlx\":\"类型1\",\"wjids\":[{\"wjid\":\"id11\"},{\"wjid\":\"id12\"},{\"wjid\":\"id13\"}]},{\"wjlx\":\"类型2\",\"wjids\":[{\"wjid\":\"id21\"},{\"wjid\":\"id22\"},{\"wjid\":\"id23\"}]}]";
        //JSONObject jsonObject = new JSONObject(json);
        //JSONObject data = jsonObject.getJSONObject("date");
        JSONArray items =new JSONArray(json);
        JSONObject  row = null;
        JSONArray  rowwjids = null;
        JSONObject  rowwjid = null;
        for (int i = 0; i < items.length(); i++) {
            row = items.getJSONObject(i);
            System.out.println("人员类型:"+row.get("wjlx"));
            rowwjids = row.getJSONArray("wjids");
            for (int j = 0; j < rowwjids.length(); j++) {
                rowwjid = rowwjids.getJSONObject(j);
                System.out.println(rowwjid.get("wjid"));
            }
            }
        }
}

已解决