• JSONObject


    JSONObject_1_3
     package json;
     
     import net.sf.json.JSON;
     import net.sf.json.JSONObject;
     import net.sf.json.xml.XMLSerializer;
     
     public class JSONObject_1_3 {
     public static void javaToJSON() {
     System.out.println("java代码封装为json字符串");
     JSONObject jsonObj = new JSONObject();
     jsonObj.put("username", "张三");
     jsonObj.put("password", "123456");
     System.out.println("java--->json \n" + jsonObj.toString());
     }
     
     public static void jsonToJAVA() {
     System.out.println("json字符串转java代码");
     String jsonStr = "{\"password\":\"123456\",\"username\":\"张三\"}";
     JSONObject jsonObj = JSONObject.fromString(jsonStr);
     String username = jsonObj.getString("username");
     String password = jsonObj.optString("password");
     System.out.println("json--->java\n username=" + username
     + "\t password=" + password);
     }
     
     public static void jsonToXML() {
     System.out.println("json字符串转xml字符串");
     String jsonStr = "{\"password\":\"123456\",\"username\":\"张三\"}";
     JSONObject json = JSONObject.fromString(jsonStr);
     XMLSerializer xmlSerializer = new XMLSerializer();
     xmlSerializer.setRootName("user_info");
     xmlSerializer.setTypeHintsEnabled(false);
     String xml = xmlSerializer.write(json);
     System.out.println("json--->xml \n" + xml);
     }
     
     public static void javaBeanToJSON() {
     System.out.println("javabean转json字符串");
     UserInfo userInfo = new UserInfo();
     userInfo.setUsername("张三");
     userInfo.setPassword("123456");
     JSONObject json = JSONObject.fromBean(userInfo);
     System.out.println("javabean--->json \n" + json.toString());
     }
     
     public static void javaBeanToXML() {
     System.out.println("javabean转xml字符串");
     UserInfo userInfo = new UserInfo();
     userInfo.setUsername("张三");
     userInfo.setPassword("123456");
     JSONObject json = JSONObject.fromBean(userInfo);
     XMLSerializer xmlSerializer = new XMLSerializer();
     String xml = xmlSerializer.write(json, "UTF-8");
     System.out.println("javabean--->xml \n" + xml);
     }
     
     public static void xmlToJSON(){
     System.out.println("xml字符串转json字符串");
     String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><user_info><password>123456</password><username>张三</username></user_info>";
     JSON json=XMLSerializer.read(xml);
     System.out.println("xml--->json \n"+json.toString());
     }
     
     public static void main(String args[]) {
     // javaToJSON();
     // jsonToJAVA();
     // jsonToXML();
     // javaBeanToJSON();
     // javaBeanToXML();
     xmlToJSON();
     }
     }

    json.jarjson_new.jar
    
    

  • 相关阅读:
    sqlserver查询数据的所有表名和行数
    java内存查看与分析
    jboss中JVM监控
    建设一个能承受500万PV/每天的网站如果计算?
    Java MVC框架性能比较
    struts1,struts2,springMVC终极对比
    struts2的action是线程安全的,struts1的action不是线程安全的真正原因
    浅析Struts1和Struts2的Action线程安全问题
    xml bug之cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration...
    eclipse调试web项目
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/2985660.html
Copyright © 2020-2023  润新知