zoukankan      html  css  js  c++  java
  • json 数据格式的转换

    json 与对象的互相转换 以下为整理内容

    1  前台

    //将JSON转为字符串
    var aToStr=JSON.stringify(a);
    
    //将字符串转为JSON格式
    var bToObj=JSON.parse(b);
    
    //将字符串转为JSON格式
    var dataObj=eval("("+data+")");
    
    //得到json
    $.get(url, [data], [callback], "json")

    2  后台

    第一种:json-lib 包    https://www.cnblogs.com/zhangminghui/p/4107735.html

    //JSONSerializer
    
     JSON json = JSONSerializer.toJSON(book);
    
     Object tempObj =  JSONSerializer.toJava(json);
    
      //  JSONArray 将集合list 数组变为json 
    
        JSONArray jsonList = JSONArray.fromObject(list);
    
      //JSONObject  将对象, map 集合变为json
      JSONObject jsonObject1 = JSONObject.fromObject( map );  
    
      JSONObject jsonObj2 = JSONObject.fromObject( book );  
       
       //JSON转为BEAN
       Object bean = JSONObject.toBean( jsonObject );  
    
       //处理xml
    
            JSONObject json = new JSONObject( true );
            XMLSerializer xmlSerializer = new XMLSerializer();
            String xml = xmlSerializer.write( json ); 
            System.out.println("构建简单XML文件");
            System.out.println(xml);
            JSONObject json2 = JSONObject.fromObject("{"name":"json","bool":true,"int":1}");  
            String xml2 = new XMLSerializer().write( json2 ); 
            System.out.println("构建稍复杂XML文件");
            System.out.println(xml2);
            JSONArray json3 = JSONArray.fromObject("[1,2,3]");  
            String xml3 = new XMLSerializer().write( json3 ); 
            System.out.println("构建数字XML文件");
            System.out.println(xml3);
            JSONArray json4 = (JSONArray) new XMLSerializer().readFromFile(new File("src/json.xml"));  
            System.out.println("通过JSON解析XML文件");
            System.out.println( json4 );  
        

    2  阿里巴巴的fastjson   https://www.cnblogs.com/LiuChunfu/p/5099248.html

    
    
    //将对象转换为JSON字符串
      String strJson=JSON.toJSONString(info);

    public void test3(){
    
     String json="{"name":"chenggang","age":24}";
     //反序列化
     UserInfo userInfo=JSON.parseObject(json,UserInfo.class);
     System.out.println("name:"+userInfo.getName()+", age:"+userInfo.getAge());
     
    }
    /**泛型的反序列化*/
    public static void test4(){
      String json="{"user":{"name":"zhangsan","age":25}}";
      Map<String, UserInfoBean> map = JSON.parseObject(json, new TypeReference<Map<String, UserInfoBean>>(){});
      System.out.println(map.get("user"));
     }
    
    
    Date date=new Date();  
      //输出毫秒值
      System.out.println(JSON.toJSONString(date));
      //默认格式为yyyy-MM-dd HH:mm:ss  
      System.out.println(JSON.toJSONString(date, SerializerFeature.WriteDateUseDateFormat));
      //根据自定义格式输出日期 
      System.out.println(JSON.toJSONStringWithDateFormat(date, "yyyy-MM-dd", SerializerFeature.WriteDateUseDateFormat));
    Fastjson API入口类是com.alibaba.fastjson.JSON,常用的序列化操作都可以在JSON类上的静态方法直接完成。
    //  public static final Object parse(String text); // 把JSON文本parse为JSONObject或者JSONArray 
    //  public static final JSONObject parseObject(String text); // 把JSON文本parse成JSONObject    
    //  public static final <T> T parseObject(String text, Class<T> clazz); // 把JSON文本parse为JavaBean 
    //  public static final JSONArray parseArray(String text); // 把JSON文本parse成JSONArray 
    //  public static final <T> List<T> parseArray(String text, Class<T> clazz); //把JSON文本parse成JavaBean集合 
    //  public static final String toJSONString(Object object); // 将JavaBean序列化为JSON文本 
    //  public static final String toJSONString(Object object, boolean prettyFormat); // 将JavaBean序列化为带格式的JSON文本 
    //  public static final Object toJSON(Object javaObject); 将JavaBean转换为JSONObject或者JSONArray(和上面方法的区别是返回值是不一样的)

    3 com.fasterxml.jackson  包Jackson 

     https://www.cnblogs.com/jiangbei/p/6881787.html

    //导包 对象转
            //创建ObjectMapper对象
            ObjectMapper mapper = new ObjectMapper();
    Map madeList= this.madeList(menuList);
    String jsonStr = mapper.writeValueAsString(madeList);
    //  Customer cust = new Customer("jackson", "1001"); 
    String jsonStr
    = mapper.writeValueAsString(cust);
    System.
    out.println(jsonStr); List<Customer> list = (List) Arrays.asList(cust,new Customer("33","离魂计"));
    String jsonStrList
    = mapper.writeValueAsString(list);
    System.
    out.println(jsonStrList);
    /** * jackson使用getter方法定位属性 * 可以通过添加注解 @JsonIgnore 使某些getter来进行忽略 */

    //json 转对象 ObjectMapper mapper = new ObjectMapper();
    String json
    = "{"name":"jackson","id":"1001"}";
    Customer c
    = mapper.readValue(json, Customer.class); System.out.println(c.getId());
  • 相关阅读:
    js 笔记
    openstack笔记
    Nginx
    Nginx
    Nginx
    nginx 服务器篇
    Nginx 原理篇
    MySQL 视图、触发器、函数、存储过程
    day41
    MySQL 作业题及答案
  • 原文地址:https://www.cnblogs.com/jsbk/p/10054107.html
Copyright © 2011-2022 走看看