xiejun
2025-01-23 618caa4a9f759fbc871085eca90791e869151bdc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package com.test;
 
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
 
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
 
public class Test {
 
    public static void main(String[] args) {
         String name="zhangsan";
 
         int age=15;
 
         Long l=(long) 1.04;
 
         Double doub=1.555;
         Date date=new Date();
 
         Timestamp timeTemp=new Timestamp(System.currentTimeMillis());
 
         float f=3.5f;
 
         char c='a';
 
         boolean flag=false;
 
         short s=(short) 5.6;
 
        DataObject dataObject=new DataObject(name, age, l, doub, date, timeTemp, f, c, flag, s);
        List<DataObject> dataList=new ArrayList<DataObject>();
        //dataList.add(dataObject);
        //dataList.add(dataObject);
        JsonObject json=new JsonObject();
        //json.setDataList(dataList);
        Object object = JSONObject.toJSON(dataObject);
        String resultStr = object.toString();
        System.out.println(resultStr);
        Map<String,Object> masterDataList = new HashMap<String,Object>();
        masterDataList = JSONArray.parseObject(resultStr,Map.class);
 
        //for (Map<String, Object> map : masterDataList.keySet()) {
            for (String key:masterDataList.keySet()) {
                 Object object2 = masterDataList.get(key);
                 String objectToStr = objectToStr(object2);
                 System.out.println(key+"--¡·"+objectToStr);
 
            }
 
        //}
        /*for (Map<String, Object> map : masterDataList) {
            for (String key:map.keySet()) {
                 Object object2 = map.get(key);
                 String objectToStr = objectToStr(object2);
                 System.out.println(key+"--¡·"+objectToStr);
            }
 
        }*/
 
    }
 
    private static String objectToStr(Object vaule) {
        String newValue="";
        if(vaule ==null||"".equals(vaule)||vaule==""){
            return "";
        }
        try {
            if (vaule instanceof Integer) {
                Integer intValue = (Integer) vaule;
                newValue = String.valueOf(intValue);
            } else if (vaule instanceof Boolean) {
                boolean boolValue = (Boolean) vaule;
                newValue = Boolean.toString(boolValue);
            } else if (vaule instanceof Timestamp) {
                Timestamp timestampValue = (Timestamp) vaule;
                newValue = timestamp2Str(timestampValue);
            } else if (vaule instanceof Date) {
                Date dataValue = (Date) vaule;
                newValue = date2Str(dataValue, "yyyy-MM-dd HH:mm:ss");
            }else if (vaule instanceof BigDecimal){
                BigDecimal bigDecimal=(BigDecimal)vaule;
                newValue =  bigDecimal.toString();
            } else {
                return vaule.toString();
            }
        }catch (Throwable e){
        //    log.error("ÊôÐÔÀàÐÍת»»´íÎó",e);
            e.printStackTrace();
        }
        return newValue;
    }
 
    private static String date2Str(Date date, String format) {
        if (null == date) {
            return null;
        } else {
            if (format == null || format.trim().length() == 0) {
                format = "yyyy-MM-dd HH:mm:ss.SSS";
            }
 
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.format(date);
        }
    }
 
    private static String timestamp2Str(Timestamp time) {
        Date date = null;
        if (null == time) {
            return null;
        } else {
            if (null != time) {
                date = new Date(time.getTime());
            }
 
            return date2Str(date, "yyyy-MM-dd HH:mm:ss.SSS");
        }
    }
 
}