HTTP请求的返回值通常是以json字符串的形式,服务端在接收到返回值时,需要解析返回值得到想要的结果,包括状态码、数据等。这里介绍一下如何解析json字符串,获取其中的参数,以及将某个参数转换成复杂的List
这样的类型。
需要使用jar包:
net.sf.json-lib
json-lib
2.4
jdk15
com.fasterxml.jackson.core
jackson-databind
下面是示例代码,分别将json字符串转换成JSONObject、从JSONObject中获取参数、将JSONObject转换成map、将JSONObject里的参数转换成List。
public static String parseStrFromJson(JSONObject jsonObject, String key) {
return jsonObject.getString(key);
}
public static String parseStrFromJson(JSONObject jsonObject, String key) {
return jsonObject.getString(key);
}
public static String parseStrFromJson(JSONObject jsonObject, String key) {
return jsonObject.getString(key);
}
private static List parseListFromJson(JSONObject jsonObject, String key) {
JSONArray jsonArray = jsonObject.getJSONArray(key);
List commentList = new ArrayList<>();
//commentList = (List)jsonArray; // 强制转换类型虽然可以成功,但是List里的对象将变成JSONObject而不是想要的Comment类型
// 如果使用强制转换,那么在for-each语句中将会报错net.sf.json.JSONObject cannot be cast to com.example.study.demo.beans.Comment
if (jsonArray != null && jsonArray.size() > 0) {
ObjectMapper objectMapper = new ObjectMapper(); // 需要另外导入jar包:jackson-databind
for (Object obj : jsonArray) {
try {
Comment comment = objectMapper.readValue(JSONObject.fromObject(obj).toString(), Comment.class);
commentList.add(comment);
} catch (IOException e) {
e.printStackTrace();
}
}
}
return commentList;
}
package com.example.study.demo.http;
import com.example.study.demo.beans.Comment;
import com.fasterxml.jackson.databind.ObjectMapper;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* json字符串解析与类型转换示例
*/
public class JsonParseTest {
/**
* 将json类型的字符串转换成json对象
* @param jsonStr
* @return
*/
public static JSONObject parseStrToJsonObj(String jsonStr) {
return JSONObject.fromObject(jsonStr);
}
/**
* 根据key,以String的形式获取对应字段的值
* @param jsonObject
* @param key
* @return
*/
public static String parseStrFromJson(JSONObject jsonObject, String key) {
return jsonObject.getString(key);
}
/**
* 将json对象转换成map
* @param jsonObject
* @return
*/
private static Map parseMapFromJson(JSONObject jsonObject) {
return (Map)jsonObject;
}
/**
* 将某个key对应的值转换成List
* @param jsonObject
* @param key
* @return
*/
private static List parseListFromJson(JSONObject jsonObject, String key) {
JSONArray jsonArray = jsonObject.getJSONArray(key);
List commentList = new ArrayList<>();
//commentList = (List)jsonArray; // 强制转换类型虽然可以成功,但是List里的对象将变成JSONObject而不是想要的Comment类型
// 如果使用强制转换,那么在for-each语句中将会报错net.sf.json.JSONObject cannot be cast to com.example.study.demo.beans.Comment
if (jsonArray != null && jsonArray.size() > 0) {
ObjectMapper objectMapper = new ObjectMapper(); // 需要另外导入jar包:jackson-databind
for (Object obj : jsonArray) {
try {
Comment comment = objectMapper.readValue(JSONObject.fromObject(obj).toString(), Comment.class);
commentList.add(comment);
} catch (IOException e) {
e.printStackTrace();
}
}
}
return commentList;
}
public static void main(String[] args) {
// 某http接口返回的json格式的数据
String str = "{\"code\":\"200\",\"msg\":\"OK\"," +
"\"data\":[{\"commentId\":\"289389291\",\"newsId\":\"DSQ8CB7G0001875P\",\"content\":\"呵呵呵,还有约炮约出来打牌的,\",\"author\":\"湿人小冰\",\"releaseTime\":1538125092000,\"url\":\"https://news.163.com/18/0928/16/DSQ8CB7G0001875P.html\",\"sourceId\":8446,\"params\":\"a2869674571f77b5a0867c3d71db5856\",\"vote\":66},{\"commentId\":\"289485529\",\"newsId\":\"DSQ8CB7G0001875P\",\"content\":\"想起我表弟,一个在浙江做门的回到家里结婚生子,回家创业做门生意,老实巴交的人,父母也很贫穷,我们都为他这个儿子感到高兴。 好好的生意不去做,去给赌场放哨,一天几百块 后来负载26万跑路,亲戚10多万,赌场高利贷10多万。妻离子散,父母都是泪\",\"author\":\"用心做好产品振兴中国制造\",\"releaseTime\":1538127470000,\"url\":\"https://news.163.com/18/0928/16/DSQ8CB7G0001875P.html\",\"sourceId\":8446,\"params\":\"a2869674571f77b5a0867c3d71db5856\",\"vote\":27},{\"commentId\":\"290271813\",\"newsId\":\"DSQ8CB7G0001875P\",\"content\":\"什么意思?读不懂\",\"author\":\"王氏s1jy\",\"releaseTime\":1538188295000,\"url\":\"https://news.163.com/18/0928/16/DSQ8CB7G0001875P.html\",\"sourceId\":8446,\"params\":\"a2869674571f77b5a0867c3d71db5856\",\"vote\":2}]" +
",\"currentPage\":1,\"commentNum\":5}";
JSONObject jsonObject = parseStrToJsonObj(str);
String code = parseStrFromJson(jsonObject, "code"); // 获取String
System.out.println("code:" + code);
System.out.println("-----分隔符----");
Map map = parseMapFromJson(jsonObject); // 将json对象转换成map
System.out.println(map.get("msg"));
System.out.println("-----分隔符----");
List commentList = parseListFromJson(jsonObject, "data");
if (commentList != null && commentList.size() > 0) {
for (Comment c : commentList) {
System.out.println(c.getCommentId() + ":"+ c.getAuthor());
}
}
System.out.println("....");
}
}
Connected to the target VM, address: '127.0.0.1:53719', transport: 'socket'
code:200
-----分隔符----
OK
-----分隔符----
Disconnected from the target VM, address: '127.0.0.1:53719', transport: 'socket'
289389291:湿人小冰
289485529:用心做好产品振兴中国制造
290271813:王氏s1jy
....
Process finished with exit code 0