这个借助了

import com.fasterxml.jackson.databind.JavaType;

使用下面这个方法做转换,可以说是一个工具吧,注意一下,constructParametricType()这个方法其实已经作废了

public static JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClasses) {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);
}

下面来转一下,这里的PsOrg是一个POJO

                String returnObj = jso.get("returnObj").toString();
                JavaType javaType = getCollectionType(ArrayList.class, PsOrg.class);
                List<PsOrg> psOrgList = (List<PsOrg>) objectMapper.readValue(returnObj, javaType);

还有一些其他方法,比如google的Gson,方法里的str就是一个json类型的字符串

Gson gson = new Gson();
List<String> mfgLines = gson.fromJson(str, new TypeToken<List<String>>() {}.getType());

还有个更方便的方法,使用阿里的fastjson,只需要一行代码

List<Model> list = JSON.parseArray("你的json字符串", Model.class); (Model是你的实体)
如果两边的字段数量不一致,可以在你的实体类上添加下面注解

@JsonIgnoreProperties(ignoreUnknown = true)




附录:我们看一下那个作废的方法以及新的替换方法
源码

/**
     * @deprecated Since 2.5, use {@link #constructParametrizedType} instead.
     */
    @Deprecated
    public JavaType constructParametricType(Class<?> parametrized, Class<?>... parameterClasses) {
        return constructParametrizedType(parametrized, parametrized, parameterClasses);
    }

/**
     * Factory method for constructing {@link JavaType} that
     * represents a parameterized type. For example, to represent
     * type <code>List<Integer></code>, you could
     * call
     *<pre>
     *  TypeFactory.constructParametrizedType(List.class, List.class, Integer.class);
     *</pre>
     *<p>
     * The reason for first two arguments to be separate is that parameterization may
     * apply to a super-type. For example, if generic type was instead to be
     * constructed for <code>ArrayList<Integer></code>, the usual call would be:
     *<pre>
     *  TypeFactory.constructParametrizedType(ArrayList.class, List.class, Integer.class);
     *</pre>
     * since parameterization is applied to {@link java.util.List}.
     * In most cases distinction does not matter, but there are types where it does;
     * one such example is parameterization of types that implement {@link java.util.Iterator}.
     *<p>
     * NOTE: type modifiers are NOT called on constructed type itself; but are called
     * when resolving <code>parameterClasses</code> into {@link JavaType}.
     *
     * @param parametrized Type-erased type of instance being constructed
     * @param parametersFor class or interface for which type parameters are applied; either
     *   <code>parametrized</code> or one of its supertypes
     * @param parameterClasses Type parameters to apply
     * 
     * @since 2.5
     */
    public JavaType constructParametrizedType(Class<?> parametrized, Class<?> parametersFor,
            Class<?>... parameterClasses)
    {
        int len = parameterClasses.length;
        JavaType[] pt = new JavaType[len];
        for (int i = 0; i < len; ++i) {
            pt[i] = _fromClass(parameterClasses[i], null);
        }
        return constructParametrizedType(parametrized, parametersFor, pt);
    }
可以仔细看一下注释,说的很清晰,我还没尝试这个




GitHub 加速计划 / js / json
41.72 K
6.61 K
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:1 个月前 )
960b763e 4 个月前
8c391e04 6 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐