Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
Simple-Sms
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
zhangchengbo
Simple-Sms
Commits
361e89b7
Commit
361e89b7
authored
Nov 18, 2024
by
zhangchengbo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix:添加Json转换工具类
parent
3061a3b5
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
237 additions
and
0 deletions
+237
-0
GsonUtil.java
...n/kotlin/com/simplemobiletools/commons/util/GsonUtil.java
+237
-0
No files found.
commons/src/main/kotlin/com/simplemobiletools/commons/util/GsonUtil.java
0 → 100644
View file @
361e89b7
package
com
.
simplemobiletools
.
commons
.
util
;
import
android.text.TextUtils
;
import
android.util.ArrayMap
;
import
com.google.gson.Gson
;
import
com.google.gson.JsonObject
;
import
com.google.gson.reflect.TypeToken
;
import
org.json.JSONException
;
import
org.json.JSONObject
;
import
java.lang.reflect.Type
;
import
java.util.ArrayList
;
import
java.util.LinkedHashMap
;
import
java.util.List
;
import
java.util.Map
;
/**
* @创建者 Blizzard-liu
* @创建时间 2017/5/9 11:04
* @描述 ${TODO}
* @更新者 ${Author}
* @更新时间 2017/5/9
* @更新描述 ${compile 'com.google.code.gson:gson:2.2.4'}
*/
public
class
GsonUtil
{
/**
* 把一个map变成json字符串
*
* @param map
* @return
*/
public
static
String
parseMapToJson
(
Map
<?,
?>
map
)
{
try
{
Gson
gson
=
new
Gson
();
return
gson
.
toJson
(
map
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
null
;
}
public
static
String
parseListToJson
(
List
<?>
list
)
{
try
{
Gson
gson
=
new
Gson
();
return
gson
.
toJson
(
list
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
null
;
}
/**
* 把一个json字符串变成对象
*
* @param json
* @param cls
* @return
*/
public
static
<
T
>
T
parseJsonToBean
(
String
json
,
Class
<
T
>
cls
)
{
T
t
=
null
;
try
{
Gson
gson
=
new
Gson
();
t
=
gson
.
fromJson
(
json
,
cls
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
t
;
}
public
static
String
parseBeanToJson
(
Object
cls
)
{
Gson
gson
=
new
Gson
();
return
gson
.
toJson
(
cls
);
}
/**
* 把json字符串变成map
*
* @param json
* @return
*/
public
static
ArrayMap
<
String
,
Object
>
parseJsonToMap
(
String
json
)
{
Gson
gson
=
new
Gson
();
Type
type
=
new
TypeToken
<
LinkedHashMap
<
String
,
Object
>>()
{
}.
getType
();
ArrayMap
<
String
,
Object
>
map
=
null
;
try
{
map
=
gson
.
fromJson
(
json
,
type
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
map
;
}
/**
* 把json字符串变成集合
* <p>
* params: new TypeToken<List<yourbean>>(){}.getType(),
*
* @param json
* @param type new TypeToken<List<yourbean>>(){}.getType()
* @return
*/
public
static
List
<?>
parseJsonToList
(
String
json
,
Type
type
)
{
Gson
gson
=
new
Gson
();
List
<?>
list
=
gson
.
fromJson
(
json
,
type
);
return
list
;
}
public
static
<
T
>
List
<
T
>
listFromJson
(
String
json
){
Gson
gson
=
new
Gson
();
return
gson
.
fromJson
(
json
,
new
TypeToken
<
List
<
T
>>(){}.
getType
());
}
/**
* 获取json串中某个字段的值,注意,只能获取同一层级的value
*
* @param json
* @param key
* @return
*/
public
static
String
getFieldValue
(
String
json
,
String
key
)
{
if
(
TextUtils
.
isEmpty
(
json
))
return
""
;
if
(!
json
.
contains
(
key
))
return
""
;
JSONObject
jsonObject
=
null
;
String
value
=
null
;
try
{
jsonObject
=
new
JSONObject
(
json
);
value
=
jsonObject
.
getString
(
key
);
if
(
value
.
equals
(
"null"
))
value
=
""
;
}
catch
(
JSONException
e
)
{
e
.
printStackTrace
();
}
return
value
;
}
public
static
<
T
>
ArrayList
<
T
>
jsonToArrayList
(
String
json
,
Class
<
T
>
clazz
)
{
ArrayList
<
T
>
arrayList
=
new
ArrayList
<>();
if
(!
json
.
isBlank
()
&&
!
json
.
isEmpty
())
{
Type
type
=
new
TypeToken
<
ArrayList
<
JsonObject
>>()
{
}.
getType
();
ArrayList
<
JsonObject
>
jsonObjects
=
new
Gson
().
fromJson
(
json
,
type
);
for
(
JsonObject
jsonObject
:
jsonObjects
)
{
arrayList
.
add
(
new
Gson
().
fromJson
(
jsonObject
,
clazz
));
}
}
return
arrayList
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment