JSONデータはJavaオブジェクトに変換されます.

9875 ワード

一、JSON概要
JSON(JavaScript Object Notation)は、軽量級のデータ交換フォーマットです.それはJavaScriptのサブセットに基づいています.
JSONは簡単に言うと、JavaScriptのオブジェクトと配列です.
i.対象:対象はJavaScriptにおいて「{}」として拡張された内容を表し、データ構造は「{key:value,key:value、...」のキー値ペアの構造である.このときのオブジェクトは、オブジェクト指向の言語ではMap<String、Object>に相当し、keyが対象の属性であり、valueは対応する属性値である.「city」:「Beijing」、「street」:「Choyang Road」、「postcode」:10025}
ii.配列:配列はJavaScriptにおいて、中かっこ「[]」の拡張内容である.abc、12345、false、nullなどです.
二、JavaScriptはJSONデータを処理する
JSONはテキスト、すなわち文字列として伝えられていますが、JavaScriptはJSONオブジェクトを操作していますので、JSON文字列をJSONオブジェクトに変換する必要があります.
JSON文字列:var str 1='{"name":"cxh","sex":"man"'; 
JSON対象:var str 2={name}:“cxh”、“sex”:“man”} 
変換関数:var str 2=eval('('+str 1+') 注:もしvar str 1=「{name':'cxh'、'sex':'man'」;var str 2=eval(「(」+str 1+「)」)
三、JavaScriptはJSONデータを読みだします.
JavaScriptはJSONオブジェクトを取得すると、そのオブジェクトの値を直接読み込むことができます.
例えば:
var j={"name":"Michael","address":
      {「city」:「Beijing」、「street」:「Choyang Road」、「postcode」:10025}
  }; 
  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'}]
次のモデルを書く必要があります.
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);  
    }    
}