Demo1
package com.activity;
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.app.Activity;
import android.os.Bundle;
import android.os.Environment;
/**
* android中复杂的json数据的操作,
* @author Administrator
*
*/
public class JsonProjectActivity extends Activity {
/** Called when the activity is first created. */
private String nameData[]=new String[]{"小马哥","QQ","腾讯"};
private int ageData[]=new int[]{30,5,7};
private boolean isMarraiedData[]=new boolean[]{false,true,false};
private Date brithdayData[]=new Date[]{new Date(),new Date(),new Date()};
private double salaryData[]=new double[]{3000.0,5000.0,9000.0};
private String companyName="北京qq";
private String companyAddr="深圳南山科技园";
private String companyTel="小马哥的电话13994512345";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
JSONObject allData = new JSONObject();// 建立最外面的节点对象
JSONArray sing = new JSONArray();// 定义数组
for (int x = 0; x < nameData.length; x++) {// 将数组内容配置到相应的节点
JSONObject temp = new JSONObject();// 每一个包装的数据都是jsonojbect
try {
temp.put("name", nameData[x]);
temp.put("age", ageData[x]);
temp.put("married", isMarraiedData[x]);
temp.put("salary", salaryData[x]);
temp.put("birthday", brithdayData[x]);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sing.put(temp);// 保存多个jsonobject对象
}
try {
allData.put("personData", sing);
allData.put("company", this.companyName);
allData.put("address", this.companyAddr);
allData.put("telephone", this.companyTel);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (!Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
return;
}
File file = new File(Environment.getExternalStorageDirectory()
+ File.separator + "qqdata" + File.separator + "qq.txt");
if (!file.getParentFile().exists()) {// 文件不存在
file.getParentFile().mkdirs();// 创建文件夹
}
PrintStream outStream = null;
try {
outStream = new PrintStream(new FileOutputStream(file));
outStream.print(allData.toString());// 将数据变为字符串后保存
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (outStream != null) {
outStream.close();
}
}
}
}
Demo2
package com.activity;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
/**
* android中json数据的操作
* @author Administrator
*
*/
public class JsonProjectActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String data[] = new String[] { "www.qq.com", "yy", "QQ" };
JSONObject allData = new JSONObject();// 建立最外面的节点对象
JSONArray sing = new JSONArray();// 定义数组
for (int x = 0; x < data.length; x++) {// 将数组内容配置到相应的节点
JSONObject temp = new JSONObject();// 每一个包装的数据都是jsonojbect
try {
temp.put("myurl", data[x]);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sing.put(temp);// 保存多个jsonobject对象
}
try {
allData.put("urldata", sing);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (!Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
return;
}
File file = new File(Environment.getExternalStorageDirectory()
+ File.separator + "qqdata" + File.separator + "qq.txt");
if (!file.getParentFile().exists()) {// 文件不存在
file.getParentFile().mkdirs();// 创建文件夹
}
PrintStream outStream = null;
try {
outStream = new PrintStream(new FileOutputStream(file));
outStream.print(allData.toString());// 将数据变为字符串后保存
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (outStream != null) {
outStream.close();
}
}
}
}