将JSONObject写入JSONfile [英] Write to JSONObject to JSONfile

查看:591
本文介绍了将JSONObject写入JSONfile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,该应用程序必须接收字符串,将其编码为JSONObject格式,然后将其写入SD中的JSON文件中.除了编写部分以外,一切似乎都工作正常.我的清单中有<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />命令,这就是我的代码

I have an app that has to take a string, encode it into JSONObject format and write it into a JSON file in the SD. It all seems to be working fine apart from writing part. I have <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />command in my MANIFEST and that's my code

 btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          //  String lines[] =  importantemail.split("\\r?\\n");
          //  String firstLine = (lines[0]);
            //String secondLine = (lines[1]);
          //  Toast.makeText(SignificantEmailActivity.this,firstLine + secondLine,Toast.LENGTH_SHORT).show();;
            JSONObject jsonObject = makeJsonObject();
            try{
                Writer output = null;
                File file = new File(Environment.getExternalStorageDirectory()+ "importantemail.json");
                if (!file.exists()) {
                    file.mkdirs();
                }
                output = new BufferedWriter(new FileWriter(file));
                output.write(jsonObject.toString());
                output.close();
                Toast.makeText(getApplicationContext(), "Composition saved", Toast.LENGTH_LONG).show();

            } catch (Exception e) {
                Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show();
            }
            finish();


        }
    });
    }
     public JSONObject makeJsonObject()
     {
         JSONObject object = new JSONObject();
         try {
             object.put("Message ID", id);
             object.put("Sender",accountStr);
             object.put("Subject",subj);
             object.put("E-mail:",importantemail);
         }catch (JSONException e)
         {
             e.printStackTrace();
         }
         return object;
     }

当我按下按钮时,我会从Toast收到此消息 存储/仿真/0importantemail.json 没有权限" 不知道为什么会这样

When I press the button I get this message from the Toast "storage/emulated/0importantemail.json Permission denied" No idea why is that though

推荐答案

经过测试,您好,请替换几行,这将解决您的问题.

Tested, Hi replace few lines and this will fix your issue.

  JSONObject jsonObject = makeJsonObject();
    try{
        Writer output = null;
        File file = new File(Environment.getExternalStorageDirectory(), "importantemail.json");
        if(file.isDirectory()){
            file.delete();
        }
        if(!file.exists()){
            file.createNewFile();
        }
        output = new BufferedWriter(new FileWriter(file));
        output.write(jsonObject.toString());
        output.close();
        Toast.makeText(getApplicationContext(), "Composition saved", Toast.LENGTH_LONG).show();

    } catch (Exception e) {
        Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show();
    }
    finish();


}
public JSONObject makeJsonObject()
{
    JSONObject object = new JSONObject();
    try {
        object.put("Message ID", 5);
        object.put("Sender","test");
        object.put("Subject","test");
        object.put("E-mail:","test");
    }catch (JSONException e)
    {
        e.printStackTrace();
    }
    return object;
}

这篇关于将JSONObject写入JSONfile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆