行业资讯 2025年06月7日
0 收藏 0 点赞 169 浏览 4963 个字
摘要 :

JSON与JAVA数据的相互转换 net.sf.json.JSONArray;  net.sf.json.JSONException;  net.sf.json.JSONObject;  net.sf.json.JSONSerializer;   &……

JSON与JAVA数据相互转换

net.sf.json.JSONArray;
 net.sf.json.JSONException;
 net.sf.json.JSONObject;
 net.sf.json.JSONSerializer;
    
    
    
    @Test
      jsonToJava(){
        String json="[{"addTime":"2011-09-19 14:23:02","iccid":"1111","id":0,"imei":"2222","imsi":"3333","phoneType":"4444","remark":"aaaa","tel":"5555"}]";
                (json.indexOf("[")!=-1){
            json=json.replace("[", "");
        }
        (json.indexOf("]")!=-1){
            json=json.replace("]", "");
        }
        JSONObject obj= JSONObject().fromObject(json);
        SimInfo simInfo=(SimInfo)JSONObject.toBean(obj, SimInfo.);
        System.out.println("obj: "+simInfo);
        System.out.println(simInfo.getAddTime());
        System.out.println(simInfo.getIccid());
        System.out.println(simInfo.getImei());
        System.out.println(simInfo.getImsi());
        System.out.println(simInfo.getPhoneType());
        System.out.println(simInfo.getRemark());
        System.out.println(simInfo.getTel());
        System.out.println(simInfo.getId());
        
        DaoFactory.getSimInfoDao().add(simInfo);
    }

    
    @Test
      jsonToJavas(){
        String jsons="[{"addTime":"2011-09-19 14:23:02","iccid":"1111","id":0,"imei":"2222","imsi":"3333","phoneType":"4444","remark":"aaaa","tel":"5555"}," +
                     "{"addTime":"2011-11-11 14:23:02","iccid":"2222","id":0,"imei":"2222","imsi":"3333","phoneType":"4444","remark":"aaaa","tel":"5555"}]";
        List<SimInfo> simInfos = getJavaCollection( SimInfo(),jsons);
        System.out.println(simInfos.size());
        (SimInfo simInfo:simInfos){
            System.out.println("addTime: "+simInfo.getAddTime());
            System.out.println("=========");
        }
        
    }

    
     <T> List<T> getJavaCollection(T clazz, String jsons) {
        List<T> objs=;
        JSONArray jsonArray=(JSONArray)JSONSerializer.toJSON(jsons);
        (jsonArray!=){
            objs= ArrayList<T>();
            List list=(List)JSONSerializer.toJava(jsonArray);
            (Object o:list){
                JSONObject jsonObject=JSONObject.fromObject(o);
                T obj=(T)JSONObject.toBean(jsonObject, clazz.getClass());
                objs.add(obj);
            }
        }
         objs;
    }
    
    
    @Test
      javaToJson(){
        SimInfo simInfo= SimInfo();
        simInfo.setAddTime(UtilTool.dateToStr( Date(), ));
        simInfo.setIccid("1111");
        simInfo.setImei("2222");
        simInfo.setImsi("3333");
        simInfo.setPhoneType(4);
        simInfo.setRemark("aaaa");
        simInfo.setTel("5555");
                String json= JSONArray().fromObject(simInfo).toString();
                System.out.println("json: "+json);
    }
StringWriter writer =  StringWriter();
            ObjectMapper mapper =  ObjectMapper();
            mapper.writeValue(writer, simInfo);
            String json=writer.toString();
// 基于json-lib.jar包Json实例程序

String json = "{name="json",bool:true,int:1,double:2.2}";
JSONObject jsonObject = JSONObject.fromObject(json);
Object bean = JSONObject.toBean(jsonObject);
assertEquals(jsonObject.get("name"), PropertyUtils.getProperty(bean, "name"));
assertEquals(jsonObject.get("bool"), PropertyUtils.getProperty(bean, "bool"));
assertEquals(jsonObject.get("int"), PropertyUtils.getProperty(bean, "int"));
assertEquals(jsonObject.get("double"), PropertyUtils.getProperty(bean, "double"));
String json = "{name:"zhangsan",age:25,hight:1.72,sex:true}";
JSONObject jsonObject = JSONObject.fromObject(json);
UserBean bean = (UserBean) JSONObject.toBean(jsonObject, UserBean.);
System.out.println(jsonObject);
理论上,这样就可以了,但时,有异常Caused by: java.lang.NoSuchMethodException: com.json.Json$UserBean.<init>()
             

String json = "["first","second"]";
JSONArray jsonArray = (JSONArray) JSONSerializer.toJSON(json);
List output = (List) JSONSerializer.toJava(jsonArray);
     

String json = "["first","second"]";
JSONArray jsonArray = (JSONArray) JSONSerializer.toJSON(json);
JsonConfig jsonConfig =  JsonConfig();
jsonConfig.setArrayMode(JsonConfig.MODE_OBJECT_ARRAY);
Object[] output = (Object[]) JSONSerializer.toJava(jsonArray, jsonConfig);
Object[] expected =  Object[] { "first", "second" };
ArrayAssertions.assertEquals(expected, output);
5.JSON 字符串 专为 JavaBean(刘慧斌demo 演示需要的jar包在附件里)

String str="[{"id":"328","mestype":"inbox"},{"id":"327","mestype":"inbox"},{"id":"279","mestype":"already"},{"id":"278","mestype":"already"},{"id":"277","mestype":"already"},{"id":"310","mestype":"inbox"},{"id":"308","mestype":"inbox"},{"id":"305","mestype":"inbox"},{"id":"304","mestype":"inbox"},{"id":"303","mestype":"inbox"}]";
  JSONArray  jsonArray=(JSONArray) JSONSerializer.toJSON(str);
  List list=(List)JSONSerializer.toJava(jsonArray);
         (Object  obj: list) {
         JSONObject jsonObject = JSONObject.fromObject(obj);
      MessageBean bean = (MessageBean) JSONObject.toBean(jsonObject, MessageBean.);
   String id=bean.getId()+"";
   String type=bean.getMestype();
   System.out.println(id+" "+type);
  }
  System.out.println(list.size());

微信扫一扫

支付宝扫一扫

版权: 转载请注明出处:https://www.zuozi.net/4079.html

管理员

相关推荐
2025-06-07

在数字化阅读与实体书籍并存的时代,图书租赁管理系统正成为图书馆、书店及共享书吧提升运营效率的…

953
2025-06-07

在当今快节奏的校园生活中,外卖已成为学生和教职工日常生活中不可或缺的一部分。随着外卖需求的不…

997
2025-06-07

“金融市场瞬息万变,能否用Python实现同花顺自动化交易?” 这个问题,正是当下许多投资者和技术开发…

777
2025-06-07

你是否想过用中文编写一款高效、稳定的多用户聊天软件? 对于中小型团队或个人开发者而言,*易语言*…

1,018
2025-06-07

一、系统架构设计 分层架构: 前端层:用户端(H5/小程序/APP)+ 管理后台(Web) 服务层:抽奖核心…

674
2025-06-07

在数字化时代,二手交易市场正以前所未有的速度蓬勃发展。无论是闲置物品的流通,还是环保意识的提…

469
发表评论
暂无评论

还没有评论呢,快来抢沙发~

助力内容变现

将您的收入提升到一个新的水平

点击联系客服

在线时间:08:00-23:00

客服QQ

122325244

客服电话

400-888-8888

客服邮箱

122325244@qq.com

扫描二维码

关注微信客服号