json数组和List集合转换总结

复制代码
//方法一,将json解析后赋给单独的元素
String jstr="{'json':'jsonvalue','bool':true,'int':1,'double':'20.5'}";
JSONObject json=JSONObject.fromObject(jstr);
boolean bool=json.getBoolean("bool");
int i=json.getInt("int");
double d=json.getDouble("double");
String value=json.getString("json");
System.out.println("bool="+String.valueOf(bool)+"\tjson="+value+"\tint="+i+"\tdouble="+d);
复制代码
 
  
复制代码
//方法二,json对象和bean对象之间相互转换
//假如你是有一个bean对象
class User{
private String name;
private String psw;
//封装getter/setter省略
}

//把一个json对象变成user
String u="{'name':'sail331x','psw':'123456789'}";
//用user对象接收json中的元素
User user=(User)JSONObject.toBean(JSONObject.fromObject(u),User.class);

//把一个user变成json对象:
JSONObject juser=JSONObject.fromObject(user);
//json字符串jstr
String jstr=juser.toString();
复制代码
复制代码
//方法三,json对象直接转list集合、list集合转json对象
//声明一个Person 实体类:
import java.util.Date;
public class Person {
    String id;
    int age;
    String name;
    Date birthday;
    //get,set方法省略
}

//测试json数据转换
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':'zhuangsan','age':18},{'name':'lisi','age':20}]";
    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':'zhuangsan','age':18},{'name':'lisi','age':20}]";
    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':'zhuangsan','age':20},{'name':'lisi','age':18}]";
    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字符串数组:
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);   } }
复制代码

JSONArray jsonArray=null;
if(edge != null && !"".equals(edge)){
jsonArray = JSONArray.fromObject(edge);
}


experimentService.deleteEdgeByExperimentId(experimentId);
if (jsonArray != null && !"".equals(jsonArray)) {
for (int i = 0; i < jsonArray.size(); i++) {
Object o = jsonArray.get(i);
JSONObject jsonObject = JSONObject.fromObject(o);
Edge edgeob = (Edge) JSONObject.toBean(jsonObject, Edge.class);
edgeob.setEdgeId(UuidUtil.get32UUID());
edgeob.setExperimentId(experimentId);
experimentService.insertEdge(edgeob);
}
}

你可能感兴趣的:(json数组和List集合转换总结)