android中Json資料儲存方式
阿新 • • 發佈:2019-02-18
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;
}
}