JAVA 数组格式的json字符串转换成List

时间:2024-04-15 10:28:24

一.

import org.codehaus.jackson.type.TypeReference;
import org.codehaus.jackson.map.ObjectMapper;

ObjectMapper mapper = new ObjectMapper();
List<Object> list = mapper.readValue(message,
    new TypeReference<List<Object>>() {});

二.

1.maven 

    <dependency>
        <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
        <version>2.4</version>
        <classifier>jdk15</classifier> 
    </dependency>

1.实现

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

Object[] obj = getJosnToArray(message, TmTransportSysncVehicleInfo.class);
public static <T> Object[] getJosnToArray(String jsonString, Class<T> classz) {
    // net.sf.jso
    JSONArray array = JSONArray.fromObject(jsonString);
    Object[] obj = new Object[array.size()];
    for (int i = 0; i < array.size(); i++) {
        JSONObject jsonObject = array.getJSONObject(i);
        obj[i] = JSONObject.toBean(jsonObject, classz);
    }
    return obj;
}