读写Json格式的数据
json格式的数据我们经常用于网络与客户端之间的数据交流,json相对于传统的xml,更加简洁,所以应用也越来越广泛,也常被用于本地存储
Android SDK用于读写JSON格式的数据的主要的两个类:JsonWriter和JsonReader,可以处理json对象和json数组,处理json对象的代码必须放在beginObject方法和endObject方法中,处理json数组的代码必须放在beginArray方法和endArray方法中,看下面代码:
public class TextActivitiy extends Activity{
private String jsonfile = "/sdcard/text.json";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_json);
}
private class Product{
public String name;
public String sex;
public Product(String name,String sex){
this.name = name;
this.sex = sex;
}
}
/**
* 将Json对象写入文件
* @param writer
* @param product
* @throws IOException
*/
public void writeProduct(JsonWriter writer,Product product) throws IOException{
writer.beginObject();
writer.name("name").value(product.name);
writer.name("sex").value(product.sex);
writer.endObject();
}
/**
* 将Json数组写入文件
* @param writer
* @param products
* @throws IOException
*/
public void writeProductArray(JsonWriter writer,List<Product> products) throws IOException{
writer.beginArray();
for(Product product : products){
writeProduct(writer, product);
}
writer.endArray();
}
/**
* 将数组写入OutputStream中
* @param os
* @param products
* @throws IOException
*/
public void writeArrayStream(OutputStream os,List<Product> products) throws IOException{
JsonWriter writer = new JsonWriter(new OutputStreamWriter(os));
writer.setIndent(" ");
writeProductArray(writer, products);
writer.close();
}
public void onClick_WriteJSON(View view){
try {
//打开要写入的文件
FileOutputStream fos = new FileOutputStream(jsonfile);
List<Product> products = new ArrayList<Product>();
products.add(new Product("姚化吉", "男"));
products.add(new Product("刘欣煜", "男"));
writeArrayStream(fos, products);
Toast.makeText(getApplicationContext(), "数据写入成功", 1).show();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 读取Json对象
* @param reader
* @return
* @throws IOException
*/
public Product readProduct(JsonReader reader) throws IOException{
String name = null;
String sex = null;
reader.beginObject();
while(reader.hasNext()){
String role = reader.nextName();
if(role.equals("name")){
name = reader.nextString();
}else if(role.equals("sex")){
sex = reader.nextString();
}else{
reader.skipValue();
}
}
reader.endObject();
return new Product(name, sex);
}
/**
* 读取Json数组
* @param reader
* @return
* @throws IOException
*/
public List<Product> readProductArray(JsonReader reader) throws IOException{
List<Product> products = new ArrayList<Product>();
reader.beginArray();
while(reader.hasNext()){
products.add(readProduct(reader));
}
reader.endArray();
return products;
}
/**
* 从文件中读取json数组并转化为List<Product> 对象
* @param in
* @return
* @throws Exception
*/
public List<Product> readArrayStream(InputStream in) throws Exception{
JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
try{
return readProductArray(reader);
}finally{
reader.close();
}
}
public void onClick_ReadJSON(View view){
String result = "";
try {
//指定读取json数据的文件路径
FileInputStream fis = new FileInputStream(jsonfile);
List<Product> products = readArrayStream(fis);
if(products != null){
for(Product product : products){
result += "name:" + product.name + "sex:" + product.sex + "\n";
}
Toast.makeText(getApplicationContext(), result, 2).show();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这段代码主要是将List数组保存到JSON格式的文件中,再将该文件中的内容读取出来并还原成list数组