android中Json数据保存方式
来源:互联网 发布:温湿度数据记录器 编辑:程序博客网 时间:2024/06/10 05:31
package com.example.savejsonproject;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Date;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
private String company="宜昌静哥科技软件学院";
private String address="湖北省宜昌市宜都市枝城镇";
private String telephone="18671736137";
private String[] namedata={"李元静","冯新尧","何帆"};
private int[] agedata={21,22,21};
private boolean[] marrieddata={true,false,true};
private double[] salarydata={8000.0,8000.0,8000.0};
private Date[] birthdaydata={new Date(),new Date(),new Date()};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_main);
JSONObject allData=new JSONObject(); //建立最外面的JSONObject
JSONArray array=new JSONArray(); //定义新的JSONArray对象
for (int i = 0; i < namedata.length; i++) { //For循环添加数据
JSONObject temp=new JSONObject(); //创建一个新的JSONObject对象
try {
temp.put("name", namedata[i]); //设置要保存的数据,直接子项的子项
temp.put("age", agedata[i]);
temp.put("merried", marrieddata[i]);
temp.put("salary", salarydata[i]);
temp.put("birthday", birthdaydata[i]);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
array.put(temp);
}
try {
allData.put("persondata", array); //保存所有数据
allData.put("company", this.company); //最外层数据
allData.put("address", this.address);
allData.put("telephone", this.telephone);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){//判断是否存在SD卡
return ;
}
File file=new File(Environment.getExternalStorageDirectory().toString()
+File.separator+"mldndata"
+File.separator+"json.txt");
if(!file.getParentFile().exists()){//判断父文件是否存在,如果不存在则创建
file.getParentFile().mkdirs();
}
PrintStream out=null; //打印流
try {
out=new PrintStream(new FileOutputStream(file)); //实例化打印流对象
out.print(allData.toString()); //输出数据
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(out!=null){ //如果打印流不为空,则关闭打印流
out.close();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Date;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
private String company="宜昌静哥科技软件学院";
private String address="湖北省宜昌市宜都市枝城镇";
private String telephone="18671736137";
private String[] namedata={"李元静","冯新尧","何帆"};
private int[] agedata={21,22,21};
private boolean[] marrieddata={true,false,true};
private double[] salarydata={8000.0,8000.0,8000.0};
private Date[] birthdaydata={new Date(),new Date(),new Date()};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_main);
JSONObject allData=new JSONObject(); //建立最外面的JSONObject
JSONArray array=new JSONArray(); //定义新的JSONArray对象
for (int i = 0; i < namedata.length; i++) { //For循环添加数据
JSONObject temp=new JSONObject(); //创建一个新的JSONObject对象
try {
temp.put("name", namedata[i]); //设置要保存的数据,直接子项的子项
temp.put("age", agedata[i]);
temp.put("merried", marrieddata[i]);
temp.put("salary", salarydata[i]);
temp.put("birthday", birthdaydata[i]);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
array.put(temp);
}
try {
allData.put("persondata", array); //保存所有数据
allData.put("company", this.company); //最外层数据
allData.put("address", this.address);
allData.put("telephone", this.telephone);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){//判断是否存在SD卡
return ;
}
File file=new File(Environment.getExternalStorageDirectory().toString()
+File.separator+"mldndata"
+File.separator+"json.txt");
if(!file.getParentFile().exists()){//判断父文件是否存在,如果不存在则创建
file.getParentFile().mkdirs();
}
PrintStream out=null; //打印流
try {
out=new PrintStream(new FileOutputStream(file)); //实例化打印流对象
out.print(allData.toString()); //输出数据
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(out!=null){ //如果打印流不为空,则关闭打印流
out.close();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
- android中Json数据保存方式
- android 数据保存方式
- Android中json数据手动解析方式
- Android 通过JSON保存数据
- Android中常用的有四种保存数据的方式
- Android轻量级数据保存方式
- Android手机数据保存方式
- Android中解析json数据的方式之一:Gson
- Android中解析json数据的三种方式
- android 保存Json数据到本地
- Android 使用 JSON 保存应用数据
- Android api 数据的保存方式
- Android 用文件存储方式保存数据
- 关于数据保存的四中方式
- 论Android中关于如何保存activity的状态,以及保存的数据如何取出的方式
- Android JSON数据解析(GSON方式)
- ajax中解析json数据的方式
- Android中常用的有四种保存方式
- Different between "user", "eng" and "userdebug" build
- PCB布板过程对PCB图进行审查的原则
- 设置linux环境变量LANG
- jQuery性能优化28条建议
- linux mysql 大小写问题
- android中Json数据保存方式
- UIGestureRecognizer学习笔记
- MSVC與CRT的恩怨情仇
- 【原】写在2012年的第一天
- 【原】java内存区域理解-初步了解
- QT图形开发
- Win32学习笔记——位图的使用
- 【原】java问题排查常用linux命令
- 【转】java运维相关