Android数据存储之JSON数据解析(输出部分)

JSON是一种比Xml更简洁的数据格式,它免去了xml许多不必要的标签信息,使得数据储存更高效。在Android中已经集成了JSON包,可以直接使用。

Andriod的JSON解析需要掌握最主要的两个类:JSONObject 与 JSONArray

主Activity组件等准备,为了简单直观,就设置一个按钮:

View Code
public class MainActivity extends Activity {

    private Button saveJSONBtn = null;    
    private File file = null;
    
    // 准备好要输出的数据
    private String feature  = "smart";
    private String favorite = "moka";
    private String[] names = new String[] {"shuai1", "shuai2", "shuai3"};
    private int[] id = new int[] {1, 2, 3};
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        saveJSONBtn = (Button) findViewById(R.id.saveJSONBtn);
        saveJSONBtn.setOnClickListener(new SaveJSONListener());
        
    }

按钮的绑定事件:

View Code
    private class SaveJSONListener implements OnClickListener {

        @Override
        public void onClick(View v) {
             // 首先调用检测储存环境的方法
            if(!MainActivity.this.CheckEnvironment()) {
                return;
            } else {
                generateJSON();
                Toast.makeText(MainActivity.this, "成功输出JSON数据", Toast.LENGTH_SHORT);
            }
        }
        
    }

检测储存环境环境方法:

View Code
     /* 检测储存环境是否配置正常,并设置文件存放路径 */
    private boolean CheckEnvironment() {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            file = new File(Environment.getExternalStorageDirectory().toString() + File.separator + "documents"
                    + File.separator + "JSON.txt");
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            return true;
        } else {
            return false;
        }
    }

生成JSON数据文件的方法:

View Code
    private void generateJSON() {
        FileOutputStream fos = null;
        PrintStream ps = null;
        try {
            // 准备输出流
            fos = new FileOutputStream(file);
            ps = new PrintStream(fos);
            
            // 实例化一个JSONObject, 放入单个数据
            JSONObject myJSONData = new JSONObject();
            myJSONData.put("feature", "smart");
            myJSONData.put("favorite", "moka");
            // 实例化一个myJSONArray,放入数组数据
            JSONArray myJSONArray = new JSONArray();
            for (int i = 0; i < names.length; i++) {
                JSONObject temp = new JSONObject();
                temp.put("name", names[i]);
                temp.put("id", id[i]);
                myJSONArray.put(temp);
            }
            myJSONData.put("info", myJSONArray);
            
            // 输出JSON格式数据
            ps.print(myJSONData.toString());
            
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        } finally {
            try {
                if (ps != null || fos != null) {
                    ps.close();
                    ps = null;
                    fos.close();
                    fos = null;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
}

生成文件:

原文地址:https://www.cnblogs.com/moka/p/3070676.html