android中 把json字符串从文件中去除,转换为一个 自定义对象的list中
来源:互联网 发布:java中的字符串反转 编辑:程序博客网 时间:2024/05/22 06:35
主要类:(这里用到一个gson。要想libs导入gson的一个jar)
public class FileConfigActivity extends Activity {
// private static final String JSONString =
// "[{\"name\":\"Michael\",\"age\":20},"
// + "{\"name\":\"Mike\",\"age\":21}]";
Button writeJsonButton;
Button readJsonButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
writeJsonButton = (Button) findViewById(R.id.btnWriteJson);
readJsonButton = (Button) findViewById(R.id.btnReadJson);
writeJsonButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
writeJsonFile();
}
});
readJsonButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
readJsonFile();
}
});
}
// 写json
public void writeJsonFile() {
JSONCreateDemoActivity jsoncreate = new JSONCreateDemoActivity();
jsoncreate.WriteJson();
}
// 读json
public void readJsonFile() {
String fileContent = readAssetsFileString();
// 返回json转换好的对象list
List<Configuration> userslist = parseConfigFromJson(fileContent);
for (int i = 0; i < userslist.size(); i++) {
Configuration co = userslist.get(i);
Toast.makeText(this, Integer.toString(co.getID()),
Toast.LENGTH_LONG).show();
Toast.makeText(this, co.getLineColor(), Toast.LENGTH_LONG).show();
Toast.makeText(this, co.getFillColor(), Toast.LENGTH_LONG).show();
Toast.makeText(this, Integer.toString(co.getLineSpan()),
Toast.LENGTH_LONG).show();
Toast.makeText(this, Integer.toString(co.getLineWidth()),
Toast.LENGTH_LONG).show();
Toast.makeText(this, Boolean.toString(co.getDisplay()),
Toast.LENGTH_LONG).show();
Toast.makeText(this, Boolean.toString(co.getSelected()),
Toast.LENGTH_LONG).show();
}
}
public List<Configuration> parseConfigFromJson(String jsonData) {
Type listType = new TypeToken<List<Configuration>>() {
}.getType();
Gson gson = new Gson();
List<Configuration> userslist = gson.fromJson(jsonData, listType);
return userslist;
}
public String readAssetsFileString() {
String str = null;
try {
InputStream is = getAssets().open("about.txt");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
str = new String(buffer);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return str;
}
}
==============
对象 的类为:一个configuration类:
这个类的属性名和json中的字段一一对应:
public class Configuration {
private int ID;
private String LineColor;
private String FillColor;
private int LineSpan;
private int LineWidth;
private boolean Display;
private boolean Selected;
public int getID() {
return ID;
}
public void setID(int ID){
this.ID=ID;
}
public String getLineColor(){
return LineColor;
}
public void setLineColor(String LineColor){
this.LineColor=LineColor;
}
public String getFillColor() {
return FillColor;
}
public void setFillColor(String FillColor) {
this.FillColor=FillColor;
}
public int getLineSpan() {
return LineSpan;
}
public void setLineSpan(int LineSpan) {
this.LineSpan=LineSpan;
}
public int getLineWidth(){
return LineWidth;
}
public void setLineWidth(int LineWidth){
this.LineWidth=LineWidth;
}
public boolean getDisplay(){
return Display;
}
public void setDisplay(boolean Display){
this.Display=Display;
}
public boolean getSelected() {
return Selected;
}
public void setSelected(boolean Selected){
this.Selected=Selected;
}
}
===========================
assets文件夹下的txt文件内容为一个json字符串:
[{"ID":1,"LineColor":"187 255 255","FillColor":"187 255 255","LineSpan":10,"LineWidth":1,"Display":true,"Selected":true},
{"ID":2,"LineColor":"187 255 255","FillColor":"187 255 255","LineSpan":10,"LineWidth":1,"Display":true,"Selected":true},
{"ID":3,"LineColor":"187 255 255","FillColor":"187 255 255","LineSpan":10,"LineWidth":1,"Display":true,"Selected":true},
{"ID":4,"LineColor":"187 255 255","FillColor":"187 255 255","LineSpan":10,"LineWidth":1,"Display":true,"Selected":true}
]
- android中 把json字符串从文件中去除,转换为一个 自定义对象的list中
- java中json包的使用以及字符串,map,list,自定义对象之间的相互转换
- Java中Json字符串直接转换为对象的方法(包括多层List集合)
- Js中把JSON字符串转换为JSON对象(eval()、new Function())
- Android中实现JSON字符串和JSON对象的转换
- json字符串转换为对象,json字符串中含数组
- java中List对象转换为JSON对象
- java中List对象转换为JSON对象
- java中List对象转换为JSON对象
- java中List对象转换为JSON对象
- android/java中把对象、对象bean、Collection以及字符串转换成Json格式数据
- java中把对象、对象bean、list集合、对象数组、Map和Set以及字符串转换成Json
- java中把对象、对象bean、list集合、对象数组、Map和Set以及字符串转换成Json
- java中把对象、对象bean、list集合、对象数组、Map和Set以及字符串转换成Json
- Java中把对象、对象bean、list集合、对象数组、Map和Set以及字符串转换成Json
- js中json字符串转换为json对象
- Java中Json字符串直接转换为对象(包括多层List集合)
- Java中Json字符串直接转换为对象(包括多层List集合 嵌套)
- 获得 android 中 assets文件夹下txt文件内容
- Struts2中表单数据怎样实例化 <input name=user.name>struts实现拿值方法
- HTML4和HTML5的区别
- 菜鸟学 C#调用存储过程操作oracle数据库中的表
- 《Essential C++》读书笔记(五)
- android中 把json字符串从文件中去除,转换为一个 自定义对象的list中
- 【Android进阶】嵌套TabHost (TabHost中放TabHost,类似二级目录、二级树)
- 苹果正式发布OS X Mountain Lion操作系统
- 如何写出规范的代码
- 杂记
- TXT阅读工具(为了解决广大网友的内燃之极,特做了一个专看H小说的工具)
- oracle连接数据库在GDV中显示数据
- Java异常的概念和分类
- a、b交换不用参数