Skip to content

Commit 7c43cab

Browse files
author
gyf
committed
增加一些
1 parent 1b0246a commit 7c43cab

File tree

4 files changed

+293
-1
lines changed

4 files changed

+293
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
package com.gyf.toolutils4a;
2+
3+
import org.json.JSONArray;
4+
import org.json.JSONException;
5+
import org.json.JSONObject;
6+
import org.json.JSONTokener;
7+
8+
import java.lang.reflect.Array;
9+
import java.util.Collection;
10+
import java.util.Map;
11+
12+
/**
13+
* Msg: JSON处理类 1、Map转为JSONObject 2、集合转换为JSONArray
14+
* Created by gyf on 2016/11/15.
15+
*/
16+
17+
public class JsonUtils {
18+
/**
19+
* Map转为JSON对象
20+
*
21+
* @param data Map数据
22+
* @return
23+
*/
24+
public static JSONObject mapToJson(Map<?, ?> data) {
25+
JSONObject object = new JSONObject();
26+
27+
for (Map.Entry<?, ?> entry : data.entrySet()) {
28+
String key = (String) entry.getKey();
29+
if (key == null) {
30+
throw new NullPointerException("key == null");
31+
}
32+
try {
33+
object.put(key, wrap(entry.getValue()));
34+
} catch (JSONException e) {
35+
e.printStackTrace();
36+
}
37+
}
38+
39+
return object;
40+
}
41+
42+
/**
43+
* 集合转换为JSONArray
44+
*
45+
* @param data 集合对象
46+
* @return
47+
*/
48+
public static JSONArray collectionToJson(Collection<?> data) {
49+
JSONArray jsonArray = new JSONArray();
50+
if (data != null) {
51+
for (Object aData : data) {
52+
jsonArray.put(wrap(aData));
53+
}
54+
}
55+
return jsonArray;
56+
}
57+
58+
/**
59+
* Object对象转换为JSONArray
60+
*
61+
* @param data Object对象
62+
* @return
63+
* @throws JSONException Object非数组时,抛出异常
64+
*/
65+
public static JSONArray arrayToJson(Object data) throws JSONException {
66+
if (!data.getClass().isArray()) {
67+
throw new JSONException("Not a primitive data: " + data.getClass());
68+
}
69+
final int length = Array.getLength(data);
70+
JSONArray jsonArray = new JSONArray();
71+
for (int i = 0; i < length; ++i) {
72+
jsonArray.put(wrap(Array.get(data, i)));
73+
}
74+
75+
return jsonArray;
76+
}
77+
78+
private static Object wrap(Object o) {
79+
if (o == null) {
80+
return null;
81+
}
82+
if (o instanceof JSONArray || o instanceof JSONObject) {
83+
return o;
84+
}
85+
try {
86+
if (o instanceof Collection) {
87+
return collectionToJson((Collection<?>) o);
88+
} else if (o.getClass().isArray()) {
89+
return arrayToJson(o);
90+
}
91+
if (o instanceof Map) {
92+
return mapToJson((Map<?, ?>) o);
93+
}
94+
95+
if (o instanceof Boolean || o instanceof Byte || o instanceof Character || o instanceof Double || o instanceof Float || o instanceof Integer || o instanceof Long
96+
|| o instanceof Short || o instanceof String) {
97+
return o;
98+
}
99+
if (o.getClass().getPackage().getName().startsWith("java.")) {
100+
return o.toString();
101+
}
102+
} catch (Exception ignored) {
103+
}
104+
return null;
105+
}
106+
107+
/**
108+
* 根据json字符串生成JSONObject对象
109+
*
110+
* @param json json字符串
111+
* @return 返回JSONObject对象或者空(null)
112+
*/
113+
public static JSONObject initJSONObject(String json) {
114+
115+
JSONObject jsonObject = null;
116+
try {
117+
JSONTokener jsonParser = new JSONTokener(json);
118+
jsonObject = (JSONObject) jsonParser.nextValue();
119+
} catch (Exception e) {
120+
} finally {
121+
return jsonObject;
122+
}
123+
124+
}
125+
126+
/**
127+
* 根据Key值相对应的Value值
128+
*
129+
* @param jsonObject Json对象
130+
* @param key 健值
131+
* @return 返回value值或空串("")
132+
*/
133+
public static String getString(JSONObject jsonObject, String key) {
134+
String value = "";
135+
try {
136+
value = jsonObject.getString(key);
137+
} catch (Exception e) {
138+
}
139+
140+
return value;
141+
}
142+
143+
/**
144+
* 根据Key值相对应的Value值
145+
*
146+
* @param jsonObject Json对象
147+
* @param key 健值
148+
* @return 返回value值或-1
149+
*/
150+
public static int getInt(JSONObject jsonObject, String key) {
151+
int value = -1;
152+
try {
153+
value = jsonObject.getInt(key);
154+
} catch (Exception e) {
155+
}
156+
157+
return value;
158+
}
159+
160+
/**
161+
* 根据Key值相对应的Value值
162+
*
163+
* @param jsonObject Json对象
164+
* @param key 健值
165+
* @return 返回value值或-1
166+
*/
167+
public static long getLong(JSONObject jsonObject, String key) {
168+
long value = -1;
169+
try {
170+
value = jsonObject.getLong(key);
171+
} catch (Exception e) {
172+
}
173+
174+
return value;
175+
}
176+
177+
/**
178+
* 根据Key值相对应的Value值
179+
*
180+
* @param jsonObject Json对象
181+
* @param key 健值
182+
* @return 返回value值或null
183+
*/
184+
public static JSONArray getJSONArray(JSONObject jsonObject, String key) {
185+
JSONArray arrays = null;
186+
try {
187+
arrays = jsonObject.getJSONArray(key);
188+
} catch (Exception e) {
189+
}
190+
191+
return arrays;
192+
}
193+
194+
/**
195+
* 根据Key值相对应的Value值
196+
*
197+
* @param jsonArray JsonArray对象
198+
* @param index 索引
199+
* @return 返回value值或null
200+
*/
201+
public static JSONObject getJSONObject(JSONArray jsonArray, int index) {
202+
JSONObject jsonObject = null;
203+
try {
204+
jsonObject = jsonArray.getJSONObject(index);
205+
} catch (Exception e) {
206+
}
207+
return jsonObject;
208+
}
209+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.gyf.toolutils4a;
2+
3+
import java.math.BigDecimal;
4+
5+
/**
6+
* Created by gyf on 2016/11/15.
7+
*/
8+
9+
public class MathUtil {
10+
/**
11+
* 字符串转Integer
12+
* @param numStr 字符串数字
13+
* @return int.若为null表示字符串为空或者转换错误
14+
*/
15+
public static Integer str2Integer(String numStr){
16+
if(StringUtil.isEmpty(numStr)){
17+
return null;
18+
}else {
19+
try{
20+
return Integer.parseInt(numStr);
21+
}catch (Exception e){
22+
return null;
23+
}
24+
}
25+
}
26+
27+
/**
28+
* 判断数值是否相等
29+
* @param num 数值
30+
* @param numStr 字符串数值
31+
* @return boolean:true相等,false否
32+
*/
33+
public static boolean numEqualsNumstr(int num, String numStr) {
34+
Integer num2 = str2Integer(numStr);
35+
return null != num2 && num == num2;
36+
}
37+
38+
39+
/**
40+
* 获取四舍五入的小数
41+
* @param num 数字字符串
42+
* @param scale 保留小数位
43+
*/
44+
public static String getScaleData(String num, int scale){
45+
if(StringUtil.isEmpty(num)){
46+
num= "0";
47+
}
48+
BigDecimal mData = new BigDecimal(num)
49+
.setScale(scale, BigDecimal.ROUND_HALF_UP); //四舍五入,保留两位小数
50+
51+
return ""+mData;
52+
}
53+
}

toolutils4a/src/main/java/com/gyf/toolutils4a/StringUtil.java

+24
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,12 @@ public static boolean isPhone(String str) {
519519
return b;
520520
}
521521

522+
/**
523+
* 判断是否为纯数字
524+
*
525+
* @param str the str
526+
* @return the boolean
527+
*/
522528
public static boolean isNumber(String str) {
523529
Pattern pattern = Pattern.compile("[0-9]*");
524530
Matcher isNum = pattern.matcher(str);
@@ -602,6 +608,24 @@ public static boolean isConSpeCharacters(String string) {
602608
return true;
603609
}
604610

611+
/**
612+
* 生成6位随机数.
613+
*
614+
* @return the int
615+
*/
616+
public static String randomFor6(){
617+
return String.valueOf((int) ((Math.random()*9+1)*100000));
618+
}
619+
620+
/**
621+
* 生成4位随机数.
622+
*
623+
* @return the int
624+
*/
625+
public static String randomFor4(){
626+
return String.valueOf((int) ((Math.random()*9+1)*1000));
627+
}
628+
605629
private static boolean isChinese(char c) {
606630
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
607631
if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS

toolutils4a/src/test/java/com/gyf/toolutils4a/ExampleUnitTest.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
public class ExampleUnitTest {
1313
@Test
1414
public void addition_isCorrect() throws Exception {
15-
assertEquals(4, 2 + 2);
15+
int i = 0;
16+
while (i < 10000){
17+
i++;
18+
System.out.println(StringUtil.randomFor6());
19+
}
20+
21+
//assertEquals(4, 2 + 2);
1622
}
1723
}

0 commit comments

Comments
 (0)