只需要简单几个步骤,只需要demo中的DynamicBean和ReadJsonFile和Root和MainActivity类就够了。就可以解析Dynamic.jsonl了你就可以自如的读取Json文件了,
步骤1,将json文件复制到“JSON字符串转换成Java实体类(POJO)”的工具中,将json转换为JavaBean
步骤2,复制你转换的javabean到你的工程中,建立对应的java类
步骤3,建立读取json文件的将json转换为string的类。
package com.example.huanglimei.myapplication;
import android.content.Context;
import android.content.res.AssetManager;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* Created by huanglimei on 2017/11/10.
*/
public class ReadJsonFile {
public static String getJson(String fileName,Context context) {
//将json数据变成字符串
StringBuilder stringBuilder = new StringBuilder();
try {
//获取assets资源管理器
AssetManager assetManager = context.getAssets();
//通过管理器打开文件并读取
BufferedReader bf = new BufferedReader(new InputStreamReader(
assetManager.open(fileName)));
String line;
while ((line = bf.readLine()) != null) {
stringBuilder.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
return stringBuilder.toString();
}
}
步骤4:编写主类获取json的具体数据
public class MainActivity extends AppCompatActivity {
// private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String json;//获取json文件为string
//DynamicBean dynamicBean;
Gson gson = new Gson();
//读取json文件
json=ReadJsonFile.getJson("dynamic.json",MainActivity.this);
Root root=gson.fromJson(json,Root.class);
//注意root为生成的javabean中的根类
root.getDynamic().get(1).getUsername();
Toast.makeText(this,root.getDynamic().get(1).getUsername(), Toast.LENGTH_SHORT).show();
// dynamicBean=gson.fromJson(jsonStr,type);
// dynamicBean.getUsername().length();
//将Java集合转换为json
// String json2 = gson.toJson(List); System.out.println(json2);
}
}
必须要用根类才可以获取数据
json数据如下:
{"dynamic":[
{
"username":"燕潇洒",
"createTime":"04-23 13:12",
"commentNum":"45",
"content":"喔喔喔",
"headIcon":"http://img2.imgtn.bdimg.com/it/u=1576185143,2361770572&fm=214&gp=0.jpg",
"images":"http://img4.imgtn.bdimg.com/it/u=3776739438,757564394&fm=214&gp=0.jpg",
"level":"公告",
"priseNum":"23"
},
{
"username":"徐金杰",
"createTime":"04-23 13:12",
"commentNum":"45",
"content":"哩哩里",
"headIcon":"http://img4.duitang.com/uploads/item/201508/17/20150817142843_4smWc.jpeg",
"images":"http://pic.58pic.com/58pic/13/70/90/29358PICQjG_1024.jpg",
"level":"公告",
"priseNum":"43"
},{
"username":"宋亮亮",
"createTime":"04-23 13:12",
"commentNum":"23",
"content":"咕咕咕",
"headIcon":"http://img2.imgtn.bdimg.com/it/u=1576185143,2361770572&fm=214&gp=0.jpg",
"images":"http://img1.3lian.com/2015/w7/85/d/21.jpg",
"level":"重要",
"priseNum":"12"
},
{
"username":"唐梦茹",
"createTime":"04-23 13:12",
"commentNum":"450",
"content":"喵喵喵",
"headIcon":"http://img4.duitang.com/uploads/item/201508/17/20150817142843_4smWc.jpeg",
"images":"http://img4.imgtn.bdimg.com/it/u=3776739438,757564394&fm=214&gp=0.jpg",
"level":"公告",
"priseNum":"345"
},{
"username":"米述林",
"createTime":"04-23 13:12",
"commentNum":"333",
"content":"咪咪咪",
"headIcon":"http://www.haopic.me/wp-content/uploads/2015/12/2015122808171644.jpg",
"images":"http://img1.3lian.com/2015/w7/85/d/21.jpg",
"level":"重要",
"priseNum":"323"
},{
"username":"冯大双",
"createTime":"04-23 13:12",
"commentNum":"322",
"content":"么么哒",
"headIcon":"http://img.bitscn.com/upimg/allimg/c160120/1453262U560940-A5c.jpg",
"images":"http://img1.3lian.com/2015/w7/85/d/21.jpg",
"level":"重要",
"priseNum":"222"
},{
"username":"施伟",
"createTime":"04-23 13:12",
"commentNum":"12",
"content":"啦啦啦",
"headIcon":"http://www.haopic.me/wp-content/uploads/2015/12/2015122808171644.jpg",
"images":"http://img1.hq2011.com/uploads/allimg/150131/0122205Y4-0.jpg",
"level":"公告",
"priseNum":"4"
},{
"username":"宋佳利",
"createTime":"04-23 15:45",
"commentNum":"23",
"content":"呵呵呵",
"headIcon":"http://scimg.jb51.net/allimg/170330/2-1F3301636200-L.jpg",
"images":"http://img1.3lian.com/2015/w7/85/d/21.jpg",
"level":"公告",
"priseNum":"12"
},{
"username":"杨永刚",
"createTime":"04-23 13:16",
"commentNum":"12",
"content":"嘻嘻嘻",
"headIcon":"http://www.qqw21.com/article/UploadPic/2016-2/20162192155654059.jpg",
"images":"http://img1.hq2011.com/uploads/allimg/150131/0122205Y4-0.jpg",
"level":"公告",
"priseNum":"1"
},{
"username":"程森然",
"createTime":"04-22 14:12",
"commentNum":"22",
"content":"哈哈哈",
"headIcon":"http://img2.imgtn.bdimg.com/it/u=1576185143,2361770572&fm=214&gp=0.jpg",
"images":"http://pic.58pic.com/58pic/13/70/90/29358PICQjG_1024.jpg",
"level":"重要",
"priseNum":"23"
}
]
}
生成的javabean如下:
package com.example.huanglimei.myapplication;
import java.util.List;
/**
* Created by huanglimei on 2017/11/10.
*/
public class Root {
private List<DynamicBean> dynamic ;
public void setDynamic(List<DynamicBean> dynamic){
this.dynamic = dynamic;
}
public List<DynamicBean> getDynamic(){
return this.dynamic;
}
}
package com.example.huanglimei.myapplication;
/**
* Created by huanglimei on 2017/11/20.
*/
public class DynamicBean {
private String username;
private String createTime;
private String commentNum;
private String content;
private String headIcon;
private String images;
private String level;
private String priseNum;
public void setUsername(String username){
this.username = username;
}
public String getUsername(){
return this.username;
}
public void setCreateTime(String createTime){
this.createTime = createTime;
}
public String getCreateTime(){
return this.createTime;
}
public void setCommentNum(String commentNum){
this.commentNum = commentNum;
}
public String getCommentNum(){
return this.commentNum;
}
public void setContent(String content){
this.content = content;
}
public String getContent(){
return this.content;
}
public void setHeadIcon(String headIcon){
this.headIcon = headIcon;
}
public String getHeadIcon(){
return this.headIcon;
}
public void setImages(String images){
this.images = images;
}
public String getImages(){
return this.images;
}
public void setLevel(String level){
this.level = level;
}
public String getLevel(){
return this.level;
}
public void setPriseNum(String priseNum){
this.priseNum = priseNum;
}
public String getPriseNum(){
return this.priseNum;
}
}