JSON与JAVA数据的转换(一)

长平狐 发布于 2012/11/28 15:41
阅读 940
收藏 0

 JSON 格式

var people = { "programmers": [ { "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" },
{ "firstName": "Jason", "lastName":"Hunter", "email": "bbbb" },
{ "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" }
],
"authors": [
{ "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" },
{ "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" },
{ "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" }
],
"musicians": [
{ "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" },
{ "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" }

] }

js通过ajax传给后台一个json数组字符串:

前台 
var data={}; 
data.fid="001"; 
data.fname="ok"; 
var myData=$.toJSON(data); 
alert(myData); 
打印的结果是:{"fid":"001","fname":"ok"} 
然后使用jquery的ajax  
$.ajax({type:"post", url:"uWkTm.do?operate=insertPlan", 
data:myData, 

[{'prjcode':'4209222012A00','countyname':'大悟县','pro_1':'A','pro_2':'A','pro_3':'A','pro_4':'A','pro_5':'A','pro_6':'A','pro_7':'A','pro_8':'A','pro_9':'A','pro_10':'A','pro_11':'A','pro_12':'A','pro_13':'A','pro_14':'A','pro_15':'A'},{'prjcode':'4209222005A03','countyname':'大悟县','pro_1':'A','pro_2':'A','pro_3':'A','pro_4':'A','pro_5':'A','pro_6':'A','pro_7':'A','pro_8':'A','pro_9':'A','pro_10':'A','pro_11':'A','pro_12':'A','pro_13':'A','pro_14':'A','pro_15':'A'},{'prjcode':'4209222005B00','countyname':'大悟县','pro_1':'A','pro_2':'A','pro_3':'A','pro_4':'A','pro_5':'A','pro_6':'A','pro_7':'A','pro_8':'A','pro_9':'A','pro_10':'A','pro_11':'A','pro_12':'A','pro_13':'A','pro_14':'A','pro_15':'A'}]

后台接受进行遍历存储

String jsonstr = request.getParameter("jsonstr");
            JSONArray json = JSONArray.fromObject(jsonstr);
            Object[] obj=json.toArray(); 
            for(int i=0;i<obj.length;i++){  
              JSONObject object = JSONObject.fromObject(obj[i]);
              String prjcode=object.get("prjcode").toString();
              String countyname=object.getString("countyname").toString();
              String pro_1=object.getString("pro_1").toString();
              String pro_2=object.getString("pro_2").toString();
              String pro_3=object.getString("pro_3").toString();
              String pro_4=object.getString("pro_4").toString();
              String pro_5=object.getString("pro_5").toString();
              String pro_6=object.getString("pro_6").toString();
              String pro_7=object.getString("pro_7").toString();
              String pro_8=object.getString("pro_8").toString();
              String pro_9=object.getString("pro_9").toString();
              String pro_10=object.getString("pro_10").toString();
              String pro_11=object.getString("pro_11").toString();
              String pro_12=object.getString("pro_12").toString();
              String pro_13=object.getString("pro_13").toString();
              String pro_14=object.getString("pro_14").toString();
              String pro_15=object.getString("pro_15").toString();
            }  


Java代码转换成json代码 

1.       List集合转换成json代码 

List list = new ArrayList(); 

list.add( "first" ); 

list.add( "second" ); 

JSONArray jsonArray2 = JSONArray.fromObject( list ); 

2.       Map集合转换成json代码 

Map map = new HashMap(); 

map.put("name", "json"); 

map.put("bool", Boolean.TRUE); 

map.put("int", new Integer(1)); 

map.put("arr", new String[] { "a", "b" }); 

map.put("func", "function(i){ return this.arr[i]; }"); 

JSONObject json = JSONObject.fromObject(map); 

3.       Bean转换成json代码 

JSONObject jsonObject = JSONObject.fromObject(new JsonBean()); 

4.       数组转换成json代码 

boolean[] boolArray = new boolean[] { true, false, true }; 

JSONArray jsonArray1 = JSONArray.fromObject(boolArray); 



5. 一般数据转换成json代码 

JSONArray jsonArray3 = JSONArray.fromObject("['json','is','easy']" ); 

6.       beans转换成json代码 

List list = new ArrayList(); 

JsonBean2 jb1 = new JsonBean2(); 

jb1.setCol(1); 

jb1.setRow(1); 

jb1.setValue("xx"); 

JsonBean2 jb2 = new JsonBean2(); 

jb2.setCol(2); 

jb2.setRow(2); 

jb2.setValue(""); 

list.add(jb1); 

list.add(jb2); 

JSONArray ja = JSONArray.fromObject(list); 




    /* 
    * list 转换json 
    * */ 
    public  static String  listToJsonString(List results){ 
        JSONArray jsonArray= JSONArray.fromObject( results ); 
             String str=" "; 
        for(int j=0;j<10;j++){ 
                str+=jsonArray.getJSONObject(j).getString("Product_id")+" "; 
                 } 
        return str ; 
    }




原文链接:http://blog.csdn.net/longeremmy/article/details/8089620
加载中
返回顶部
顶部