直接上代码:

package com.iaiai;import java.util.ArrayList;import java.util.List;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import org.json.JSONStringer;import com.xmobileapp.pictureviewer.PictureShow.Folk;import android.app.Activity;import android.os.Bundle;import android.util.Log;/** *  * <p> * Title: MainActivity.java * </p> * <p> * E-Mail: 176291935@qq.com * </p> * <p> * QQ: 176291935 * </p> * <p> * Http: iaiai.iteye.com * </p> * <p> * Create time: 2011-10-11 * </p> *  * @author 丸子 * @version 0.0.1 */public class MainActivity extends Activity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);String json = objectToJson();Log.i("************json", json);try {ArrayList<Folk> list = jsonToObject((new JSONObject(json)).getJSONArray("object"));Log.i("************", list.get(0).getName());Log.i("************", list.get(1).getName());Log.i("************", list.get(2).getName());Log.i("************", list.get(0).getList().get(0).getName());Log.i("************", list.get(0).getList().get(0).getList().get(0).getName());} catch (JSONException e) {e.printStackTrace();}}public ArrayList<Folk> jsonToObject(JSONArray jsonArray) {ArrayList<Folk> folks = new ArrayList<Folk>();try {for (int i = 0; i < jsonArray.length(); i++) {JSONObject json = jsonArray.getJSONObject(i);Folk folk = new Folk();folk.setName(json.optString("name"));folk.setAge(json.optString("age"));folk.setCareer(json.optString("career"));folk.setSalary(json.optInt("salary"));folk.setSex(json.optString("sex").equals("male") ? true : false);if (json.optString("list") != null&& json.optString("list").length() > 0) {folk.setList(jsonToObject(new JSONArray(json.optString("list"))));}folks.add(folk);}} catch (JSONException e) {e.printStackTrace();}return folks;}public String objectToJson() {List<Folk> fsk = new ArrayList<Folk>();Folk f2 = new Folk();f2.setAge("30岁");f2.setName("比尔");f2.setSex(false);fsk.add(f2);List<Folk> fs = new ArrayList<Folk>();Folk f1 = new Folk();f1.setAge("20岁");f1.setName("赵六");f1.setSex(true);f1.setList(fsk);fs.add(f1);String jsonData = null;List<Folk> folks = new ArrayList<Folk>();Folk f = new Folk();f.setAge("10岁");f.setName("张三");f.setSex(true);f.setList(fs);folks.add(f);f = new Folk();f.setAge("11岁");f.setName("李四");f.setSex(false);folks.add(f);f = new Folk();f.setAge("12岁");f.setName("王五");f.setSex(false);folks.add(f);try {return new JSONStringer().object().key("object").value(objectToJson(folks)).endObject().toString();} catch (JSONException e) {return null;}}private JSONArray objectToJson(List<Folk> folks) {JSONArray array = new JSONArray();try {StringBuilder builder = new StringBuilder();ArrayList<String> folksData = new ArrayList<String>();for (int i = 0; i < folks.size(); i++) {Folk folk = folks.get(i);JSONObject jsonObject = new JSONObject();jsonObject.put("name", folk.getName());jsonObject.put("sex", folk.isSex() ? "male" : "female");jsonObject.put("age", folk.getAge());jsonObject.put("career", folk.getCareer());jsonObject.put("salary", folk.getSalary());if (folk.getList() != null && folk.getList().size() > 0) {jsonObject.put("list", objectToJson(folk.getList()));}folksData.add(jsonObject.toString());array.put(jsonObject);}} catch (JSONException e) {e.printStackTrace();}return array;}public class Folk {String name;String career;String age;int salary;boolean sex;List<Folk> list;public List<Folk> getList() {return list;}public void setList(List<Folk> list) {this.list = list;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getCareer() {return career;}public void setCareer(String career) {this.career = career;}public String getAge() {return age;}public void setAge(String age) {this.age = age;}public int getSalary() {return salary;}public void setSalary(int salary) {this.salary = salary;}public boolean isSex() {return sex;}public void setSex(boolean sex) {this.sex = sex;}}}


运行结果:

更多相关文章

  1. android根据字体大小设置控件高度
  2. android 合包
  3. android
  4. Android(安卓)databinding笔记
  5. android ndk编译x264开源(用于android的ffmpeg中进行软编码)
  6. Android(安卓)获得手机屏幕大小
  7. android TabLayout的应用
  8. android:动态获取权限
  9. 优雅的处理Android数据库升级的问题

随机推荐

  1. 图解垃圾算法,No,捡垃圾算法
  2. 如何在Mac上更新Chrome?
  3. 我们不一样!运维自动化典型案例观止
  4. Redis6 终于还是迎来了多线程版本!
  5. 我不配有五一
  6. 喂,业余草吗?你的生死簿管理系统我出10万买
  7. 基于 Python 的企业级运维平台开发实战!
  8. 又一个程序员“倒”下,Pandownload作者被
  9. Github 封禁了有伊朗外部贡献者的项目!
  10. 程序员又开始了疯狂的薅羊毛!