json数组和List转换


                       
使用的是json-lib.jar包

将json格式的字符数组转为List对象

Java代码   收藏代码
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package hb; 
   
import java.util.Date; 
   
public class Person { 
       
     String id; 
     int age; 
     String name; 
     Date birthday; 
     public String getId() { 
         return id; 
    
     public void setId(String id) { 
         this .id = id; 
    
     public int getAge() { 
         return age; 
    
     public void setAge( int age) { 
         this .age = age; 
    
     public String getName() { 
         return name; 
    
     public void setName(String name) { 
         this .name = name; 
    
     public Date getBirthday() { 
         return birthday; 
    
     public void setBirthday(Date birthday) { 
         this .birthday = birthday; 
    
       
}
Java代码   收藏代码
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package hb; 
   
import java.util.Iterator; 
import java.util.List; 
   
import org.junit.Test; 
   
import net.sf.json.JSONArray; 
import net.sf.json.JsonConfig; 
   
public class JsonToList { 
   
     public static void main(String[] args) { 
         String json= "[{'name':'huangbiao','age':15},{'name':'liumei','age':14}]"
         JSONArray jsonarray = JSONArray.fromObject(json); 
         System.out.println(jsonarray); 
         List list = (List)JSONArray.toCollection(jsonarray, Person. class ); 
         Iterator it = list.iterator(); 
         while (it.hasNext()){ 
             Person p = (Person)it.next(); 
             System.out.println(p.getAge()); 
        
    
       
     @Test 
     public void jsonToList1(){ 
         String json= "[{'name':'huangbiao','age':15},{'name':'liumei','age':14}]"
         JSONArray jsonarray = JSONArray.fromObject(json); 
         System.out.println(jsonarray); 
         List list = (List)JSONArray.toList(jsonarray, Person. class ); 
         Iterator it = list.iterator(); 
         while (it.hasNext()){ 
             Person p = (Person)it.next(); 
             System.out.println(p.getAge()); 
        
           
    
       
     @Test 
     public void jsonToList2(){ 
         String json= "[{'name':'huangbiao','age':15},{'name':'liumei','age':14}]"
         JSONArray jsonarray = JSONArray.fromObject(json); 
         System.out.println(jsonarray); 
         System.out.println( "------------" ); 
         List list = (List)JSONArray.toList(jsonarray, new Person(), new JsonConfig()); 
         Iterator it = list.iterator(); 
         while (it.hasNext()){ 
             Person p = (Person)it.next(); 
             System.out.println(p.getAge()); 
        
           
    
   
}

将list对象转为JSON字符串数组

Java代码   收藏代码
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package hb; 
   
import java.util.LinkedList; 
import java.util.List; 
   
import net.sf.json.JSONArray; 
   
public class ListToJson { 
   
     public static void main(String[] args) { 
         List list = new LinkedList(); 
         for ( int i= 0 ;i< 3 ;i++){ 
             Person p = new Person(); 
             p.setAge(i); 
             p.setName( "name" +i); 
             list.add(p); 
        
         JSONArray jsonarray = JSONArray.fromObject(list); 
         System.out.println(jsonarray); 
    
   
}

 打印结果

Java代码   收藏代码
?
1
[{ "age" : 0 , "birthday" : null , "id" : "" , "name" : "name0" },{ "age" : 1 , "birthday" : null , "id" : "" , "name" : "name1" },{ "age" : 2 , "birthday" : null , "id" : "" , "name" : "name2" }]

 

要将 JSON 数组转换为 Java 的 List 对象,可以使用 Fastjson 的 `JSON.parseArray()` 方法,示例代码如下: ```java import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONException; // 假设 jsonArrayString 是一个 JSON 数组字符串 try { JSONArray jsonArray = JSON.parseArray(jsonArrayString); List<Object> list = jsonArray.toJavaList(Object.class); // Object.class 为转换后的元素类型 // 转换成功,处理 list // ... } catch (JSONException e) { // 解析失败,处理异常 // ... } ``` 这段代码使用 `JSON.parseArray()` 方法将 jsonArrayString 解析为一个 JSON 数组对象 jsonArray,然后使用 `toJavaList()` 方法将 jsonArray 转换为 Java 的 List 对象。需要注意的是,`toJavaList()` 方法需要传入转换后的元素类型,如果元素类型是字符串,则可以传入 `String.class`;如果元素类型是自定义的对象,可以传入该对象的类型。如果元素类型是基本类型,则可以传入相应的基本类型(如 `int.class`)。 需要注意的是,如果 JSON 数组中的元素类型是复杂类型(如对象或数组),则 list 中的元素也是相应的 Java 对象(对象或 List)。如果 JSON 数组中的元素类型是基本类型(如字符串、整数等),则 list 中的元素为相应的 Java 类型(如 String、Integer 等)。在使用 list 中的元素时,你需要根据元素的类型来进行转换
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值