JSON转为List对象--Java

准备工作:
使用net.sf.json进行转换。所依赖的jar包如下:


JSON转为List对象--Java_第1张图片
Image 1.png

所需转换的JSON如下:
{ code: "0", msg: "", sites: [{ id: "56d03d0544029cb57d00001f", name: "XX网站", siteKey: "xxxx", pages: [{ id: "56d03d0544029cb57d00001d", name: "首页", path: "index" }, { id: "56d0424144029cb57d000023", name: "第二页", path: "page2" }] }]}


package entity;
import java.io.Serializable;import java.util.List;
/** 
* 整个站点的数据,包括sites,sites是所有网站的集合
* Created by Administrator on 2016/9/17.
*/
public class WebSite implements Serializable{    
  private String code;    
  private String msg;    
  private List sites;    
  public String getCode() {        
  return code;   
}    
public void setCode(String code) {        
  this.code = code;    
}    
  public String getMsg()
{        
  return msg;   
}    
public void setMsg(String msg) {        
  this.msg = msg;    
}    
public List getSites() {        
  return sites;   
}    
public void setSites(List sites) {        
  this.sites = sites;    
}
}

package entity;import java.io.Serializable;
import java.util.List;
/**
* 表示一个网站,其中pages代表这个网站的网页集合
* Created by Administrator on 2016/9/17.
*/
public class WebsiteSites implements Serializable{   
  private String id;    
  private String name;    
  private String siteKey;    
  private List pages;    
  public String getId() {        
  return id;    
}    
public void setId(String id) {        
  this.id = id;    
}    
public String getName() {        
  return name;    
}    
public void setName(String name) {        
  this.name = name;   
}    
public String getSiteKey() {        
  return siteKey;    
}    
public void setSiteKey(String siteKey) {        
  this.siteKey = siteKey;   
}    
public List getPages() {        
  return pages;    
}   
public void setPages(List pages) 
{                    
  this.pages = pages;    
}
} 

package entity;import java.io.Serializable;
/**
* 代表pages,表示一个网页的数据
* Created by Administrator on 2016/9/17. 
*/
 public class WebsitesSitesPages implements Serializable
 {             
 private String id;    
 private String name;    
 private String path;    
 public String getId() {        
  return id;   
 }    
 public void setId(String id) {       
   this.id = id;    
 }    
 public String getName() {        
   return name;    
}    
public void setName(String name) {        
  this.name = name;    
}    
public String getPath() {        
  return path;    
}    
public void setPath(String path) {        
  this.path = path;    
}
}

package operation;import entity.WebSite;
import entity.WebsiteSites;
import entity.WebsitesSitesPages;
import net.sf.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
/** 
* Created by Administrator on 2016/9/17. 
*/
public class JSONOperation {    
public static void main(String [] args){        
String jsonData = "{\n" +                
"  code: \"0\",\n" +                
"  msg: \"\",\n" +               
"  sites: [{\n" +                
"    id: \"56d03d0544029cb57d00001f\",\n" +                
"    name: \"XX网站\",\n" +                
"    siteKey: \"xxxx\",\n" +                
"    pages: [{\n" +                
"      id: \"56d03d0544029cb57d00001d\",\n" +               
"      name: \"首页\",\n" +                
"      path: \"index\"\n" +                
"    },\n" +               
"    {\n" +                
"      id: \"56d0424144029cb57d000023\",\n" +                
"      name: \"第二页\",\n" +                
"      path: \"page2\"\n" +                
"    }]\n" +                
"  }]\n" +                
"}";        
JSONObject jsonObject = JSONObject.fromObject(jsonData);         
Map map = new HashMap();                 
map.put("sites", WebsiteSites.class);        
map.put("pages", WebsitesSitesPages.class);        
WebSite webSite = (WebSite)JSONObject.toBean(jsonObject,    WebSite.class, map);        
System.out.println(webSite.getCode());                      
System.out.println(webSite.getSites().get(0).getName());   
}
}

以上是将JSON 转为List对象的简单流程。如果想详细了解net.sf.json的使用,api链接如下:
http://json-lib.sourceforge.net/apidocs/net/sf/json/package-summary.html

你可能感兴趣的:(JSON转为List对象--Java)