当前位置 : 主页 > 编程语言 > java >

json总结

来源:互联网 收集:自由互联 发布时间:2021-06-28
json相关操作代码 package test;import com.google.gson.Gson;import com.google.gson.reflect.TypeToken;import com.lincomb.scene.input.model.ShopNames;import com.lincomb.scene.input.model.TShop;import net.sf.json.JSONArray;import net.sf.js
json相关操作代码
package test;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lincomb.scene.input.model.ShopNames;
import com.lincomb.scene.input.model.TShop;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Package test
 * @Description: ${todo}
 * @date 2017/10/31
 */
public class JsonUtilsTest {
    public static void main(String[] args) {

        ShopNames shopNames = new ShopNames(1, "c0", "value");
        shopNames.settShops(Arrays.asList("shop1","shop2","shop3"));
        shopNames.settShops2(Arrays.asList(new TShop(1L,1,new Date()),new TShop(2L,2,new Date()),new TShop(3L,3,new Date())));

        List
 
   list = Arrays.asList(new TShop(1L,1,new Date()),new TShop(2L,2,new Date()),new TShop(3L,3,new Date()));

        /**
         * gson
         */

        // 1 当对象的字段值为空或null时,跳过该字段的json转换
        Gson gson = new Gson();
//      serializeNulls()是GsonBuilder提供的一种配置,当对象的字段值为空或null时,依然对该字段进行转换
//      Gson gson2 = new GsonBuilder().serializeNulls().create();


        //gson: object -> json(string)
        String json = gson.toJson(shopNames);
        System.out.println("gson : "+json);

        //gson: json ->object
        ShopNames shopNames2 =gson.fromJson(json,ShopNames.class);
        System.out.println("gson : object1  "+shopNames2.toString());
        System.out.println("gson : object1  "+shopNames2.gettShops2().toString());


        //gson:list
   -> json数组(String) String jsonArr=gson.toJson(list); System.out.println("jsonarray --"+jsonArr); //json数组(String) ->list List
    
      list2 = gson.fromJson(jsonArr,new TypeToken
     
      >() {}.getType()); System.out.println("list --"+list2.toString()); System.out.println("------------------------------------------"); /** * net.sf.json */ //net.sf.json: object -> json(string) JSONObject jsonObject = JSONObject.fromObject(shopNames); String json2 = jsonObject.toString(); System.out.println("net.sf.json : "+json2); //net.sf.json: json ->object JSONObject jsonObject2 = JSONObject.fromObject(json2); ShopNames shopNames3=(ShopNames)JSONObject.toBean(jsonObject2,ShopNames.class); System.out.println("net.sf.json: object2 "+shopNames3.gettShops2().toString()); //list
       -> json数组(string) JSONArray jsonarr = JSONArray.fromObject(list); String jsonArray_net=jsonarr.toString(); System.out.println("jsonarray:-->"+jsonArray_net); //json数组(string) -> list JSONArray jsonArray=JSONArray.fromObject(jsonArray_net); List
        
          list_net=(List
         
          )JSONArray.toCollection(jsonArray,TShop.class); System.out.println("list_net: -->"+list_net.toString()); /** * 补充:获取jsonObject里的jsonArry * String res = HttpUtil.post(Constant.LogicServer.URL + "/provider/service/getBoughtServiceByUserId", param); JSONObject jsonob = JSONObject.fromObject(res); if(Constant.Status.SUCCESS.equals(jsonRes.get(Constant.LogicResponseVO.CODE))) { JSONArray jsonServiceInfoList = jsonob .getJSONArray("data"); List
          
            serviceInfoList = (List
           
            ) JSONArray.toCollection(jsonServiceInfoList, ServiceInfo.class); } */ System.out.println("------------------------------------------"); /** * fastjson */ //fastjson :object -> json(string) String json3=com.alibaba.fastjson.JSONObject.toJSONString(shopNames); System.out.println("fastjson :"+json3); //fastjson :json -> object ShopNames shopNames4=com.alibaba.fastjson.JSONObject.parseObject(json3,ShopNames.class); System.out.println("fastjson :object3 "+shopNames4); //通过fastJSON拼接JSON ->json(string) com.alibaba.fastjson.JSONObject root = new com.alibaba.fastjson.JSONObject(); root.put("error", "0"); root.put("success", "1"); com.alibaba.fastjson.JSONArray dataArr=new com.alibaba.fastjson.JSONArray(); JSONObject userWJQ=new JSONObject(); userWJQ.put("username", "wjq"); userWJQ.put("position", "学生"); dataArr.add(userWJQ); root.put("data", dataArr); System.out.println(root.toString()); //list
             -> json数组(string) String jsonArr_fast=com.alibaba.fastjson.JSONArray.toJSONStringWithDateFormat(list,"yyyy-MM-dd HH:mm:ss"); System.out.println("jsonArr_fast: "+jsonArr_fast); //json数组(string) -> list List
              
                list_fastjson=com.alibaba.fastjson.JSONArray.parseArray(jsonArr_fast,TShop.class); System.out.println("list_fastjson: "+list_fastjson.toString()); /** * * 补充: json数组->JSONArray ->list
                private List
                
                 > parseJsonToList(String json){ List
                 
                  > li=new ArrayList<>(); JSONArray jsonRes = JSONArray.parseArray(json); for(int i=0;i
                  
                    map=new HashMap
                   
                    (); map.put("id", (String) jsonOne.get("id")); map.put("sort", (String) jsonOne.get("sort")); li.add(map); } return li; } */ //获取json里数据 root.get(key) } } 
                   
                  
                 
                
              
           
          
         
        
     
    
 
网友评论