JSON数据转换为Java对象

一、JSON简介
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。它基于JavaScript的一个子集。
JSON简单说就是JavaScript中的对象和数组:
i.对象:对象在JavaScript中表示为“{}”扩起来的内容,数据结构为{key:value,key:value,...}的键值对的结构。此时的对象在面向对象的语言中相当于Map<String,Object>,key为对象的属性,value为对应的属性值。如{"city":"Beijing","street":" Chaoyang Road ","postcode":100025}
ii.数组:数组在JavaScript中是中括号“[]”扩起来的内容。如["abc",12345,false,null]。

二、JavaScript处理JSON数据
JSON是以文本,即字符串的形式传递的,而JavaScript操作的是JSON对象,所以有时需要将JSON字符串转换为JSON对象。
JSON字符串:var str1 = '{ "name":"cxh", "sex":"man" }'; 
JSON对象:var str2 = { "name":"cxh", "sex":"man" }; 
转换函数:var str2 = eval('('+str1+')');  注:若var str1 = "{ 'name':'cxh','sex':'man'}"; 则var str2 = eval("("+str1+")");

三、JavaScript读取JSON数据
JavaScript获取到JSON对象后,可以直接读取该对象的值。
如:
var j={"name":"Michael","address":
      {"city":"Beijing","street":" Chaoyang Road ","postcode":100025}
  }; 
  alert(j.name); 
  alert(j.address.city);

四、将JSON数据转换为Java对象
注:在Java中没有将JSON数据转换为Java对象的函数,此时需要导入第三方包ezmorph-1.0.6.jar。(下载地址:http://sourceforge.net/projects/ezmorph/files/latest/download?source=directory)
例:将如下json数据转换为java对象。
[{name:'餐饮类',nextlayerdictlist:[{dicttype:[{name:'餐饮',piclist:[{pictype:[{screentype:'7'}]}],code:'190621',coupoucount:'759'},{name:'日韩料理',piclist:[{pictype:[{screentype:'7'}]}],code:'316122',coupoucount:'0'}]}],piclist:[{pictype:[{screentype:'7'}]}],code:'190620',coupoucount:'1017'},{name:'生活服务',nextlayerdictlist:[{dicttype:[{name:'教育培训',piclist:[{pictype:[{screentype:'7'}]}],code:'200967',coupoucount:'1'}]}],piclist:[{pictype:[{screentype:'7'}]}],code:'200966',coupoucount:'1'},{name:'婚庆',piclist:[{pictype:[{screentype:'7'}]}],code:'200960',coupoucount:'0'},{name:'美容健身',piclist:[{pictype:[{screentype:'7'}]}],code:'200952',coupoucount:'0'},{name:'休闲类',nextlayerdictlist:[{dicttype:[{name:'体育健身场馆',piclist:[{pictype:[{screentype:'7'}]}],code:'190671',coupoucount:'61'},{name:'游乐游艺',piclist:[{pictype:[{screentype:'7'}]}],code:'316119',coupoucount:'0'}]}],piclist:[{pictype:[{screentype:'7'}]}],code:'190670',coupoucount:'233'}]

则需要写如下model:

package com.hsinghsu.testJson.model;  
  
import java.util.List;  
  
  
public class DictJsonType {  
      
    private String name;  
    private String code;  
    private List<DictsJsonType> nextlayerdictlist;  
    private String coupoucount;  
    private List<PicJsonType> piclist; 
      
        
    public String getName() {    
        return name;    
    }    
    public void setName(String name) {    
        this.name = name;    
    }    
    public String getCode() {    
        return code;    
    }    
    public void setCode(String code) {    
        this.code = code;    
    }    
    public List<DictsJsonType> getNextlayerdictlist() {    
        return nextlayerdictlist;    
    }    
    public void setNextlayerdictlist(List<DictsJsonType> nextlayerdictlist) {    
        this.nextlayerdictlist = nextlayerdictlist;    
    }  
      
    public String getCoupoucount() {  
        return coupoucount;  
    }  
    public void setCoupoucount(String coupoucount) {  
        this.coupoucount = coupoucount;  
    }  
    public List<PicJsonType> getPiclist() {  
        return piclist;  
    }  
    public void setPiclist(List<PicJsonType> piclist) {  
        this.piclist = piclist;  
    }  
      
}  
package com.hsinghsu.testJson.model;  
  
import java.util.List;  
  
public class DictsJsonType {  
      
    private List<DictJsonType> dicttype;  
  
    public List<DictJsonType> getDicttype() {  
        return dicttype;  
    }  
  
    public void setDicttype(List<DictJsonType> dicttype) {  
        this.dicttype = dicttype;  
    }  
      
} 
package com.hsinghsu.testJson.model;  
  
import java.util.List;  
  
public class PicJsonType {  
    private List<PicsJsonType> pictype;  
  
    public List<PicsJsonType> getPictype() {  
        return pictype;  
    }  
  
    public void setPictype(List<PicsJsonType> pictype) {  
        this.pictype = pictype;  
    }  
}  
package com.hsinghsu.testJson.model;  
  
public class PicsJsonType {  
  
    String screentype;  
  
    public String getScreentype() {  
        return screentype;  
    }  
  
    public void setScreentype(String screentype) {  
        this.screentype = screentype;  
    }  
      
      
}
转换代码如下:
package com.hsinghsu.testJson.test;  
  
import java.util.ArrayList;  
import java.util.HashMap;  
import java.util.List;  
import java.util.Map;  
  
import net.sf.json.JSONArray;  
import net.sf.json.JSONObject;  
  
import org.junit.Test;  
  
import com.hsinghsu.testJson.model.DictJsonType;  
import com.hsinghsu.testJson.model.DictsJsonType;  
import com.hsinghsu.testJson.model.PicJsonType;  
import com.hsinghsu.testJson.model.PicsJsonType;  
  
public class TestJsonDictData {  
      
  //将json数据转换为list对象 
    private List<DictJsonType> jsonStringToList(String jsonString, Class pojoClass, String childName)    
    {    
        JSONArray jsonArray = JSONArray.fromObject(jsonString);    
        JSONObject jsonObject;    
        List<DictJsonType> list = new ArrayList<DictJsonType>();    
        Map<String, Class> map = new HashMap<String, Class>();       
          
        map.put("nextlayerdictlist", DictsJsonType.class);  
        map.put("dicttype", DictJsonType.class);  
        map.put("piclist", PicJsonType.class);  
        map.put("pictype", PicsJsonType.class);  
        for(int i = 0; i < jsonArray.size(); i++)    
        {    
            jsonObject = jsonArray.getJSONObject(i);    
            DictJsonType dictType = (DictJsonType)JSONObject.toBean(jsonObject, DictJsonType.class, map);     
            list.add(dictType);    
        }    
        return list;    
    }  
      
      
    private String resultCode = null;  
    private void getPCode(List<DictJsonType> list, String code)  
    {  
        for(DictJsonType dictType:list)//一级菜单  
        {  
            if(null != dictType.getNextlayerdictlist() && dictType.getNextlayerdictlist().size() > 0)  
            {  
                List<DictJsonType> children = dictType.getNextlayerdictlist().get(0).getDicttype();//二级菜单列表  
                if(null != children && children.size() > 0)  
                {  
                    for(DictJsonType child:children)  
                    {  
                        if(code.equals(child.getCode()))  
                        {  
                            resultCode = dictType.getCode();  
                            break;  
                        }  
                          
                        if(null != dictType.getNextlayerdictlist() && dictType.getNextlayerdictlist().size() > 0)  
                        {  
                            List<DictJsonType> childrenThree = dictType.getNextlayerdictlist().get(0).getDicttype();//二级菜单列表  
                            if(null != childrenThree && childrenThree.size() > 0)  
                            {  
                                getPCode(childrenThree, code);  
                            }  
                        }  
                    }  
                }  
            }  
        }  
    }  
        
    @Test    
    public void readJSON2List()    
    {    
        String jDataRemoveDictype="[{name:'餐饮类',nextlayerdictlist:[{dicttype:[{name:'餐饮',piclist:[{pictype:[{screentype:'7'}]}],code:'190621',coupoucount:'759'},{name:'日韩料理',piclist:[{pictype:[{screentype:'7'}]}],code:'316122',coupoucount:'0'}]}],piclist:[{pictype:[{screentype:'7'}]}],code:'190620',coupoucount:'1017'},{name:'生活服务',nextlayerdictlist:[{dicttype:[{name:'教育培训',piclist:[{pictype:[{screentype:'7'}]}],code:'200967',coupoucount:'1'}]}],piclist:[{pictype:[{screentype:'7'}]}],code:'200966',coupoucount:'1'},{name:'婚庆',piclist:[{pictype:[{screentype:'7'}]}],code:'200960',coupoucount:'0'},{name:'美容健身',piclist:[{pictype:[{screentype:'7'}]}],code:'200952',coupoucount:'0'},{name:'休闲类',nextlayerdictlist:[{dicttype:[{name:'体育健身场馆',piclist:[{pictype:[{screentype:'7'}]}],code:'190671',coupoucount:'61'},{name:'游乐游艺',piclist:[{pictype:[{screentype:'7'}]}],code:'316119',coupoucount:'0'}]}],piclist:[{pictype:[{screentype:'7'}]}],code:'190670',coupoucount:'233'}]";   
          
        List<DictJsonType> list = jsonStringToList(jDataRemoveDictype, DictJsonType.class, "nextlayerdictlist");  
        System.out.println(list.size());  
        String code = "190621";  
          
        getPCode(list, code);  
        String pCode = resultCode;  
        System.out.println("pcode:"+pCode);  
    }    
}  

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值