Android JSON的简单处理

JSON(JavaScript Object Notation)是一种数据交换格式。

保存数据到JSON文件中
  • 创建一个JSONArray集合,调用它的put方法把数据放到集合中(先创建一个toJSONF方法把数据转换成JSONObject类型,再放到集合中),再调用outputStream类,把数据写到储存空间里面去。

      /**
       * 保存crime记录到JSON
       * @param crimes
       * @throws JSONException
       * @throws IOException
       */
      public void saveCrimes(ArrayList<Crime> crimes) throws JSONException,IOException{
          //Build an array in JSON
          JSONArray array=new JSONArray();
          for (Crime c:crimes){
              array.put(c.toJSON());
          }
          //write a file in disk
          Writer writer=null;
          try{
              OutputStream out=mContext.openFileOutput(mFilename,Context.MODE_PRIVATE);
              writer.write(array.toString());
          }finally {
              if (writer!=null){
                  writer.close();
              }
          }
      }
  • 接下来在存放数据的类中编写toJSON方法:

      private static final String JSON_ID="id";
          private static final String JSON_TITLE="title";
          private static final String JSON_SOLVED="solved";
          private static final String JSON_DATE="date";
    
          ...
    
          /**
           * 把crimes记录存在JSON里面去
           * @return
           * @throws JSONException
           */
          public JSONObject toJSON() throws JSONException{
              JSONObject json=new JSONObject();
              json.put(JSON_ID,id.toString());
              json.put(JSON_TITLE,title);
              json.put(JSON_SOLVED,solved);
              json.put(JSON_DATE,date.getTime());
              return  json;
          }
  • 再在其他有关的类中调用saveCrimes方法:

      public boolean saveCrimes() {
              try {
                  mSerializer.saveCrimes(mCrimes);
                  Log.d(TAG, "crimes save to file");
                  return true;
              } catch (Exception e) {
                  Log.e(TAG, "error saving crimes", e);
                  return false;
              }
          }

+最后在activity或者fragment调用saveCrimes方法的时候应该在onPause方法中进行。

    @Override
        public void onPause() {
            super.onPause();
            CrimeLab.get(getActivity()).saveCrimes();
        }
从文件中读取JSON数据
  • 在相关的类中接受一个JSONObject对象的构造方法。

          /**
           * 从文件中读取crime数据
           * @param json
           * @throws JSONException
           */
          public Crime(JSONObject json) throws JSONException{
              id=UUID.fromString(json.getString(JSON_ID));
              if (json.has(JSON_TITLE)){
                  title=json.getString(JSON_TITLE);
              }
              solved=json.getBoolean(JSON_SOLVED);
              date=new Date(json.getLong(JSON_DATE));
          }
  • 添加一个从文件中加载数据记录的loadCrimes方法。

          /**
           * 加载crime记录的loadCrimes()方法
           * @return
           * @throws IOException
           * @throws JSONException
           */
          public ArrayList<Crime> loadCrimes() throws IOException,JSONException{
              ArrayList<Crime> crimes=new ArrayList<>();
              BufferedReader reader=null;
              try {
                  //open and read the file into a StringBuilder
                  InputStream in=mContext.openFileInput(mFilename);
                  reader=new BufferedReader(new InputStreamReader(in));
                  StringBuilder jsonString=new StringBuilder();
                  String line=null;
                  while ((line=reader.readLine())!=null){
                      //line breaks and omitted and irrelevant
                      jsonString.append(line);
                  }
                  //parse this JSON using JSONTokener
                  JSONArray array= (JSONArray) new JSONTokener(jsonString.toString()).nextValue();
                  //build the array of crimes from JSONObjects
                  for (int i = 0; i < array.length(); i++) {
                      crimes.add(new Crime(array.getJSONObject(i)));
                  }
              }catch (FileNotFoundException e){
                  //Ignore this one,it happen when starting fresh
              }finally {
                  if (reader!=null){
                      reader.close();
                  }
              }
              return crimes;
          }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值