lmw
2024-09-25 92778728b83ce1a34ba21bcdb061afdeca16cce5
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
package cn.sinata.xldutils.utils
 
import com.google.gson.JsonArray
import com.google.gson.JsonObject
import java.lang.Exception
 
/**
 *
 */
fun JsonObject.optString(key:String,defValue:String = ""):String {
    if (this.has(key)) {
        return try {
            this.get(key).asString
        } catch (e: Exception) {
            e.printStackTrace()
            defValue
        }
    }
    return defValue
}
 
fun JsonObject.optInt(key:String,defValue:Int = 0):Int {
    if (this.has(key)) {
        return try {
            this.get(key).asInt
        } catch (e: Exception) {
            e.printStackTrace()
            defValue
        }
 
    }
    return defValue
}
 
fun JsonObject.optFloat(key:String,defValue:Float = 0.0F):Float {
    if (this.has(key)) {
        return try {
            this.get(key).asFloat
        } catch (e: Exception) {
            e.printStackTrace()
            defValue
        }
    }
    return defValue
}
 
fun JsonObject.optLong(key:String,defValue:Long = 0):Long {
    if (this.has(key)) {
        return try {
            this.get(key).asLong
        } catch (e: Exception) {
            e.printStackTrace()
            defValue
        }
    }
    return defValue
}
 
fun JsonObject.optBoolean(key:String,defValue:Boolean = false):Boolean {
    if (this.has(key)) {
        return try {
            this.get(key).asBoolean
        } catch (e: Exception) {
            e.printStackTrace()
            defValue
        }
    }
    return defValue
}
 
fun JsonObject.optDouble(key:String,defValue:Double = 0.0):Double {
    if (this.has(key)) {
        return try {
            this.get(key).asDouble
        } catch (e: Exception) {
            e.printStackTrace()
            defValue
        }
    }
    return defValue
}
 
fun JsonObject.optJsonObj(key:String,defValue:JsonObject = JsonObject()):JsonObject {
    if (this.has(key)) {
        return try {
            this.get(key).asJsonObject
        } catch (e: Exception) {
            e.printStackTrace()
            defValue
        }
    }
    return defValue
}
 
fun JsonObject.optJsonArray(key:String,defValue:JsonArray = JsonArray()):JsonArray {
    if (this.has(key)) {
        return try {
            this.get(key).asJsonArray
        } catch (e: Exception) {
            e.printStackTrace()
            defValue
        }
    }
    return defValue
}