Java将其他数据类型转换成JSON字符串格式

Posted 空城余梦love

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java将其他数据类型转换成JSON字符串格式相关的知识,希望对你有一定的参考价值。

Student.java

package com.demo.servlet;

import java.util.List;
import java.util.Map;

public class Student {
    private String name;
    private Integer age;
    private List<String> love;
    private Map<String,String> school;
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public List<String> getLove() {
        return love;
    }
    public void setLove(List<String> love) {
        this.love = love;
    }
    public Map<String, String> getSchool() {
        return school;
    }
    public void setSchool(Map<String, String> school) {
        this.school = school;
    }
    
}

JsonUtil.java

package com.demo.servlet;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
 * 将其他数据类型转换成json字符串格式
 */
public class JsonUtil {  
    
    /**
     * 入口方法
     * @param obj
     * @return
     */
    public static String object2json(Object obj) {
        StringBuilder json = new StringBuilder();  
        if (obj == null) {  
            json.append("\"\"");  
        } else if (obj instanceof String || obj instanceof Integer || obj instanceof Float  
             || obj instanceof Boolean || obj instanceof Short || obj instanceof Double  
             || obj instanceof Long || obj instanceof BigDecimal || obj instanceof BigInteger  
             || obj instanceof Byte) {  
            json.append("\"").append(string2json(obj.toString())).append("\"");  
        } else if (obj instanceof Object[]) {  
            json.append(array2json((Object[]) obj));  
        } else if (obj instanceof List) {  
            json.append(list2json((List<?>) obj));  
        } else if (obj instanceof Map) {  
            json.append(map2json((Map<?, ?>) obj));  
        } else if (obj instanceof Set) {  
            json.append(set2json((Set<?>) obj));  
        } else {  
            json.append(bean2json(obj));  
        }  
        return json.toString();
    }
    
    public static String string2json(String s) {  
        if (s == null)  
            return "";  
        StringBuilder sb = new StringBuilder();  
        for (int i = 0; i < s.length(); i++) {  
            char ch = s.charAt(i);  
            switch (ch) {  
                case ‘"‘:  
                    sb.append("\\\"");
                    break;
                case ‘\\‘:
                     sb.append("\\\\");
                     break;
                 case ‘\b‘:
                     sb.append("\\b");
                     break;
                 case ‘\f‘:
                     sb.append("\\f");
                     break;
                 case ‘\n‘:
                     sb.append("\\n");
                     break;
                 case ‘\r‘:
                     sb.append("\\r");
                     break;
                 case ‘\t‘:
                     sb.append("\\t");
                     break;
                 case ‘/‘:
                     sb.append("\\/");
                     break;
                 default:
                     if (ch >= ‘\u0000‘ && ch <= ‘\u001F‘) {
                         String ss = Integer.toHexString(ch);
                         sb.append("\\u");
                         for (int k = 0; k < 4 - ss.length(); k++) {
                             sb.append(‘0‘);
                         }
                         sb.append(ss.toUpperCase());
                     } else {
                         sb.append(ch);
                     }
            }
        }
        return sb.toString();
    }
    
    public static String array2json(Object[] array) {  
        StringBuilder json = new StringBuilder();  
        json.append("[");  
        if (array != null && array.length > 0) {  
            for (Object obj : array) {  
                json.append(object2json(obj));  
                json.append(",");  
            }
            json.setCharAt(json.length() - 1, ‘]‘);  
        } else {  
            json.append("]");  
        }  
        return json.toString();
    }
    
    public static String list2json(List<?> list) {  
        StringBuilder json = new StringBuilder();  
        json.append("[");  
        if (list != null && list.size() > 0) {  
            for (Object obj : list) {  
                json.append(object2json(obj));  
                json.append(",");  
            }  
            json.setCharAt(json.length() - 1, ‘]‘);  
        } else {  
            json.append("]");  
        }  
        return json.toString();
    }
    
    public static String map2json(Map<?, ?> map) {  
        StringBuilder json = new StringBuilder();  
        json.append("{");  
        if (map != null && map.size() > 0) {  
            for (Object key : map.keySet()) {  
                json.append(object2json(key));  
                json.append(":");  
                json.append(object2json(map.get(key)));  
                json.append(",");  
            }  
            json.setCharAt(json.length() - 1, ‘}‘);  
        } else {  
            json.append("}");  
        }  
        return json.toString();  
    }
    
    public static String set2json(Set<?> set) {  
        StringBuilder json = new StringBuilder();  
        json.append("[");  
        if (set != null && set.size() > 0) {  
            for (Object obj : set) {  
                json.append(object2json(obj));  
                json.append(",");  
            }  
            json.setCharAt(json.length() - 1, ‘]‘);  
        } else { 
            json.append("]");
        }  
        return json.toString();  
    }
    
    public static String bean2json(Object bean) {  
        StringBuilder json = new StringBuilder();  
        json.append("{");  
        PropertyDescriptor[] props = null;  
        try {  
            props = Introspector.getBeanInfo(bean.getClass(), Object.class).getPropertyDescriptors();  
        } catch (IntrospectionException e) {}  
        if (props != null) {  
            for (int i = 0; i < props.length; i++) {  
                try {  
                    String name = object2json(props[i].getName());  
                    String value = object2json(props[i].getReadMethod().invoke(bean));  
                    json.append(name);  
                    json.append(":");  
                    json.append(value);  
                    json.append(",");  
                } catch (Exception e) {}  
            }  
            json.setCharAt(json.length() - 1, ‘}‘);  
         } else {  
             json.append("}");  
        }  
         return json.toString();  
    }
    
    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();  
        list.add("one");  
        list.add("two"); 
        String s1 = list2json(list);
        System.out.println("list转化成json字符串为:"+s1);//list转化成json字符串为:["one","two"]
        Set<String> set = new HashSet<String>();  
        set.add("看书");  
        set.add("旅游");
        String s2 = set2json(set);
        System.out.println("set转化成json字符串为:"+s2);//set转化成json字符串为:["旅游","看书"]
        Map<String, String> map = new HashMap<String, String>();  
        map.put("建设小学", "杨林");  
        map.put("槎市中学", "槎市");
        String s3 = map2json(map);
        System.out.println("map转化成json字符串为:"+s3);//map转化成json字符串为:{"建设小学":"杨林","槎市中学":"槎市"}
        Integer integer = new Integer(1);
        String s4 = object2json(integer);
        System.out.println("integer转化成json字符串为:"+s4);//integer转化成json字符串为:"1"
        Student student = new Student();
        student.setName("hly");
        student.setAge(22);
        student.setLove(list);
        student.setSchool(map);
        String s5 = bean2json(student);
        System.out.println("student转化成json字符串为:"+s5);//student转化成json字符串为:{"age":"22","love":["one","two"],"name":"hly","school":{"建设小学":"杨林","槎市中学":"槎市"}}
    }
}

 

以上是关于Java将其他数据类型转换成JSON字符串格式的主要内容,如果未能解决你的问题,请参考以下文章

java中把json怎么转换成数组?

java中Date转换成JSON出错如何解决?

json中的String类型的日期为啥自动转换成date类型

json

如何将数组转换成JSON-CSDN论坛

如何将C#/.NET 将json字符串格式数据转换成对象?