字符串转list 对象

引入jar包

复制代码
就可以将json转为list对象啦

<!-- https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-mapper-asl -->
<dependency>
     <groupId>org.codehaus.jackson</groupId>
     <artifactId>jackson-mapper-asl</artifactId>
     <version>1.9.13</version>
</dependency>
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

import java.io.IOException;
import java.util.List;

/**
 * JSON String转List对象
 * @author zhongpengfei
 */
public class ObjectChange {

    public static void main(String[] args) throws IOException {
        String result = "[\n" +
                "    {\n" +
                "    \"name\":\"wangwu\",\n" +
                "    \"level\":8,\n" +
                "    \"sex\":1\n" +
                "    },\n" +
                "    {\n" +
                "    \"name\":\"zhangsan\",\n" +
                "    \"level\":2,\n" +
                "    \"sex\":1\n" +
                "    },\n" +
                "    {\n" +
                "    \"name\":\"lisi\",\n" +
                "    \"level\":4,\n" +
                "    \"sex\":0\n" +
                "    }\n" +
                "]";
        ObjectMapper mapper = new ObjectMapper();
        List<Person> list = mapper.readValue(result,new TypeReference<List<Person>>(){});
        for (Person person : list) {
            System.out.println(person);
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要将字符串换为List对象集合,可以使用以下方法: 引用\[1\]中的代码展示了如何将一个包含多个元素的字符串换为List对象集合。首先,使用split()方法将字符串按照指定的分隔符(在这个例子中是逗号)分割成一个字符串数组。然后,使用Arrays.asList()方法将字符串数组换为List对象。最后,如果需要使用List集合的其他方法,可以将其换为ArrayList对象。 引用\[2\]中的代码展示了如何将一个List对象换为ArrayList对象。这是因为List接口只提供了一些基本的操作方法,如果需要使用更多的方法,可以将List对象换为ArrayList对象。 引用\[3\]中的代码展示了如何将一个不指定泛型的List对象换为指定泛型的List对象。这个例子使用了Jackson库中的ObjectMapper类的convertValue()方法。首先,创建一个ObjectMapper对象。然后,使用convertValue()方法将不指定泛型的List对象换为指定泛型的List对象。 综上所述,要将字符串换为List对象集合,可以使用split()方法将字符串分割成字符串数组,然后使用Arrays.asList()方法将字符串数组换为List对象。如果需要使用List集合的其他方法,可以将其换为ArrayList对象。另外,如果需要将不指定泛型的List对象换为指定泛型的List对象,可以使用Jackson库中的ObjectMapper类的convertValue()方法。 #### 引用[.reference_title] - *1* *2* [字符串化为 List 集合](https://blog.csdn.net/beyond513222/article/details/108211000)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [json格式字符串list集合list对象集合](https://blog.csdn.net/WangAnJiao/article/details/80811575)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杵意

谢谢金主打赏呀!!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值