概述
目录
要求
一、 使用 JSONObject / JSONArray (org.json)
核心思想(三部曲)
1. 导入 org.json 库
2. 定义String 变量 (获取需解析的数据)
3. 创建整个的JSONObject 对象
4. 获取每一个JSONObject 对象 (“孩子对象”):
5.小结
二、使用Jackson (ObjetWrapper)
1. 导入Jackson 依赖库
2. 根据数据格式,生成JavaBean对象
3.创建ObjectMapper对象
4.获取自定义的对象 (Exhibition / PostInfo /Post)
5. 根据自定义的对象,获取对应的数据
三、Gson
1. 添加Gson依赖
2. 根据数据格式,生成JavaBean对象
3. 创建Gson 对象
4.获取自定义的对象 (Exhibition / PostInfo /Post)
5. 根据自定义的对象,获取对应的数据
回顾
JSON 概述参考: https://blog.csdn.net/whjk20/article/details/108347503
要求
需要解析数据如下:
{
"pageInfo": {
"pageName": "abc",
"pagePic": "http://example.com/content.jpg"
},
"posts": [
{
"post_id": "123456789012_123456789012",
"actor_id": "1234567890",
"picOfPersonWhoPosted": "http://example.com/photo.jpg",
"nameOfPersonWhoPosted": "Jane Doe",
"message": "Sounds cool. Can't wait to see it!",
"likesCount": "2",
"comments": [],
"timeOfPost": "1234567890"
}
]
}
通过https://www.bejson.com/jshtml_format/ , 压缩为字符串:
{"pageInfo":{"pageName":"abc","pagePic":"http://example.com/content.jpg"},"posts":[{"post_id":"123456789012_123456789012","actor_id":"1234567890","picOfPersonWhoPosted":"http://example.com/photo.jpg","nameOfPersonWhoPosted":"Jane Doe","message":"Sounds cool. Can't wait to see it!","likesCount":"2","comments":[],"timeOfPost":"1234567890"}]}
一、 使用 JSONObject / JSONArray (org.json)
核心思想(三部曲)
(1) 根据字符串,创建JSONObject 对象,
public JSONObject(String source)
(2) 再根据需要,获取该它的“孩子”对象 或者 “孩子”数组对象( 可嵌套)
public JSONObject getJSONObject(String key)
public JSONArray getJSONArray(String key)
(3) 最后根据对象获取对应的字段值。
1. 导入 org.json 库
下载地址: https://mvnrepository.com/artifact/org.json/json
选择你需要的版本下载即可,例如: https://repo1.maven.org/maven2/org/json/json/20201115/json-20201115.jar
在Android studio 里,复制到libs 文件目录下, 重新同步gradle
2. 定义String 变量 (获取需解析的数据)
String src = "{"pageInfo":{"pageName":"abc","pagePic":"http://example.com/content.jpg"},"posts":[{"post_id":"123456789012_123456789012","actor_id":"1234567890","picOfPersonWhoPosted":"http://example.com/photo.jpg","nameOfPersonWhoPosted":"Jane Doe","message":"Sounds cool. Can't wait to see it!","likesCount":"2","comments":[],"timeOfPost":"1234567890"}]}";
3. 创建整个的JSONObject 对象
JSONObject jsonObject = new JSONObject(src);
4. 获取每一个JSONObject 对象 (“孩子对象”):
(1) pageInfo 对象
JSONObject pageInfo = jsonObject.getJSONObject("pageInfo");
读取字段:
String pageName = pageInfo.getString("pageName");
(2) posts 数组对象
JSONArray posts = jsonObject.getJSONArray("posts");
获取数组对象里的每一个对象("单一"对象 或者数组对象), 并且读取相应字段:
for(int i = 0; i < posts.length(); i++) {
//4. post 对象
JSONObject post = posts.getJSONObject(i);
// 字段(String)
String post_id = post.getString("post_id");
String actor_id = post.getString("actor_id");
String picOfPersonWhoPosted = post.getString("picOfPersonWhoPosted");
String nameOfPersonWhoPosted = post.getString("nameOfPersonWhoPosted");
String message = post.getString("message");
String likesCount = post.getString("likesCount");
String timeOfPost = post.getString("timeOfPost");
//String comments = post.getString("comments");
// 字段(数组)
JSONArray comments = post.getJSONArray("comments");
System.out.println("post_id="+post_id);
System.out.println("actor_id="+actor_id);
System.out.println("picOfPersonWhoPosted="+picOfPersonWhoPosted);
System.out.println("nameOfPersonWhoPosted="+nameOfPersonWhoPosted);
System.out.println("message="+message);
System.out.println("likesCount="+likesCount);
System.out.println("timeOfPost="+timeOfPost);
// System.out.println("comments="+comments);
System.out.println("comments size="+comments.length());
}
由此可看出,这种方法相对简单,适用于仅需获取数据流的部分信息。
但是要注意哪些是"单一"对象、哪些是数组对象, 其中数组对象可以包含多个"单一"对象。
5.小结
因此,根据输入字符串创建了 JSONObject 对象后:
- {} 标签对应的是单个对象,使用 getJSONObject() 获取
- [] 标签对应的是数组, 使用 getJSONArray() 获取
而针对获取到每个JSONObject 对象,也是如此处理(类似一棵树)。
二、使用Jackson (ObjetWrapper)
这种方法,主要是用到Jackson 类库的ObjetWrapper类 (package com.fasterxml.jackson.databind;)
(1) 从字符串和类名 获取该类的对象:
public <T> T readValue(String content, Class<T> valueType)
(2) 把对象转换成字符串
public String writeValueAsString(Object value)
1. 导入Jackson 依赖库
Maven官方地址: https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind
选择其中一个版本,然后在build.gradle中添加依赖:
// https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.12.1'
重新同步gradle, 以下载依赖库
2. 根据数据格式,生成JavaBean对象
在线工具: https://www.bejson.com/json2javapojo/new/
实际类的名称,可以根据需要更改。
(1) PageInfo 类:
public class PageInfo {
private String pageName;
private String pagePic;
public void setPageName(String pageName) {
this.pageName = pageName;
}
public String getPageName() {
return pageName;
}
public void setPagePic(String pagePic) {
this.pagePic = pagePic;
}
public String getPagePic() {
return pagePic;
}
}
(2) Post 类
import java.util.List;
public class Post {
private String post_id;
private String actor_id;
private String picOfPersonWhoPosted;
private String nameOfPersonWhoPosted;
private String message;
private String likesCount;
private List<String> comments;
private String timeOfPost;
public void setPost_id(String post_id) {
this.post_id = post_id;
}
public String getPost_id() {
return post_id;
}
public void setActor_id(String actor_id) {
this.actor_id = actor_id;
}
public String getActor_id() {
return actor_id;
}
public void setPicOfPersonWhoPosted(String picOfPersonWhoPosted) {
this.picOfPersonWhoPosted = picOfPersonWhoPosted;
}
public String getPicOfPersonWhoPosted() {
return picOfPersonWhoPosted;
}
public void setNameOfPersonWhoPosted(String nameOfPersonWhoPosted) {
this.nameOfPersonWhoPosted = nameOfPersonWhoPosted;
}
public String getNameOfPersonWhoPosted() {
return nameOfPersonWhoPosted;
}
public void setMessage(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
public void setLikesCount(String likesCount) {
this.likesCount = likesCount;
}
public String getLikesCount() {
return likesCount;
}
public void setComments(List<String> comments) {
this.comments = comments;
}
public List<String> getComments() {
return comments;
}
public void setTimeOfPost(String timeOfPost) {
this.timeOfPost = timeOfPost;
}
public String getTimeOfPost() {
return timeOfPost;
}
}
(3) Exhibition 类
import java.util.List;
public class Exhibition {
private PageInfo pageInfo;
private List<Post> posts;
public void setPageInfo(PageInfo pageInfo) {
this.pageInfo = pageInfo;
}
public PageInfo getPageInfo() {
return pageInfo;
}
public void setPosts(List<Post> posts) {
this.posts = posts;
}
public List<Post> getPosts() {
return posts;
}
}
3.创建ObjectMapper对象
ObjectMapper mapper = new ObjectMapper();
4.获取自定义的对象 (Exhibition / PostInfo /Post)
Exhibition exhibition = mapper.readValue(src, Exhibition.class);
PageInfo pageInfo = exhibition.getPageInfo();
List<Post> posts = exhibition.getPosts();
5. 根据自定义的对象,获取对应的数据
String pageName = pageInfo.getPageName();
for(int i = 0; i < posts.size(); i++) {
//4. post 对象
Post post = posts.get(i);
// 字段(String)
String post_id = post.getPost_id();
String actor_id = post.getActor_id();
String picOfPersonWhoPosted = post.getPicOfPersonWhoPosted();
String nameOfPersonWhoPosted = post.getNameOfPersonWhoPosted();
String message = post.getMessage();
String likesCount = post.getLikesCount();
String timeOfPost = post.getTimeOfPost();
//String comments = post.getString("comments");
// 字段(数组)
List<String> comments = post.getComments();
System.out.println("post_id="+post_id);
System.out.println("actor_id="+actor_id);
System.out.println("picOfPersonWhoPosted="+picOfPersonWhoPosted);
System.out.println("nameOfPersonWhoPosted="+nameOfPersonWhoPosted);
System.out.println("message="+message);
System.out.println("likesCount="+likesCount);
System.out.println("timeOfPost="+timeOfPost);
// System.out.println("comments="+comments);
System.out.println("comments size="+comments.size());
}
可以看出,和使用org.json 库的操作类似, 只是Jackson 需要生成与数据对应的类的对象后,再通过对象操作获取对应的数据。
三、Gson
和Jackson 类似,需要先创建与数据对应的JavaBean 类。 然后主要用的API
(1) 生成数据类对象 (根据数据字符串 和 JavaBean 类对象)
public <T> T fromJson(String json, Class<T> classOfT)
(2) 将一个Gson 对象转换成对应的字符串
public String toJson(Object src) {
1. 添加Gson依赖
build.gradle 中添加:
// https://mvnrepository.com/artifact/com.google.code.gson/gson
implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.6'
同样需要同步gradle
2. 根据数据格式,生成JavaBean对象
同上面使用Jackson 时的,略
3. 创建Gson 对象
Gson gson = new Gson();
4.获取自定义的对象 (Exhibition / PostInfo /Post)
Exhibition exhibition = gson.fromJson(src, Exhibition.class);
PostInfo / Post 的获取方式和上面使用Jackson时获取的方式是一样的,略
5. 根据自定义的对象,获取对应的数据
同上面使用Jackson 时的,略
此外,还可以把Gson 对象还原成对应的字符串: String src2 = gson.toJson(exhibition);
可以看出, 使用Gson 和 Jackson的方式类似
更多参考:
https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java
最后
以上就是忧伤翅膀为你收集整理的JSON 使用实例_解析数据的全部内容,希望文章能够帮你解决JSON 使用实例_解析数据所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复