Java “如何保存”;Data.Json“;从资产到内部存储的文件,然后将其用于读/写

Java “如何保存”;Data.Json“;从资产到内部存储的文件,然后将其用于读/写,java,android,Java,Android,目前,我正在从资产的Json文件“Data.Json”中获取包详细信息(Onnet分钟、Offnet分钟等),但我知道我们无法更改资产的值。因此,我的问题是如何将Data.json复制到内部存储,然后加载它进行读/写 我使用它从资产加载Data.Json public String loadJSONFromAsset() { String json = null; try { InputStream is = getAssets().open("Data.

目前,我正在从资产的Json文件“Data.Json”中获取包详细信息(Onnet分钟、Offnet分钟等),但我知道我们无法更改资产的值。因此,我的问题是如何将Data.json复制到内部存储,然后加载它进行读/写

我使用它从资产加载Data.Json

    public String loadJSONFromAsset() {
    String json = null;
    try {
        InputStream is = getAssets().open("Data.json");
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        json = new String(buffer, "UTF-8");
        Toast.makeText(jazz_sim_lagao_offer_details.this, "JSON Loaded", Toast.LENGTH_SHORT).show();
    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    }
    return json;
}
并使用此代码更新数据

    private void UpdateData() {
    JSONObject JSONobj = null;
    try {
        loadJSONFromAsset();
        //get JSONObject from JSON file
        JSONobj = new JSONObject(loadJSONFromAsset());
        //fetch JSONObject named
        JSONObject Jazz_SimLagaoOffer = JSONobj.getJSONObject("packages").getJSONObject("jazz_packages").getJSONObject("call_packages").getJSONObject("sim_lagao_offer");



        String Jazz_SimLagaoOffer_ONNET = Jazz_SimLagaoOffer.getString("onnet");
        Jazz_SimLagaoOffer_OnNet_TextView.setText(Jazz_SimLagaoOffer_ONNET);

        String Jazz_SimLagaoOffer_OFFNET = Jazz_SimLagaoOffer.getString("offnet");
        Jazz_SimLagaoOffer_OffNet_TextView.setText(Jazz_SimLagaoOffer_OFFNET);

        String Jazz_SimLagaoOffer_MBs = Jazz_SimLagaoOffer.getString("mbs");
        Jazz_SimLagaoOffer_Mb_TextView.setText(Jazz_SimLagaoOffer_MBs);

        String Jazz_SimLagaoOffer_SMS = Jazz_SimLagaoOffer.getString("sms");
        Jazz_SimLagaoOffer_Sms_TextView.setText(Jazz_SimLagaoOffer_SMS);


        String Jazz_SimLagaoOffer_SUBCODE = Jazz_SimLagaoOffer.getString("sub_code");
        Jazz_SimLagaoOffer_Sub_Code_TextView.setText(Jazz_SimLagaoOffer_SUBCODE);


        String Jazz_SimLagaoOffer_CHECKCODE = Jazz_SimLagaoOffer.getString("check_code");
        Jazz_SimLagaoOffer_Check_Code_TextView.setText(Jazz_SimLagaoOffer_CHECKCODE);

        String Jazz_SimLagaoOffer_UNSUBCODE = Jazz_SimLagaoOffer.getString("unsub_code");
        Jazz_SimLagaoOffer_Unsub_Code_TextView.setText(Jazz_SimLagaoOffer_UNSUBCODE);

        Jazz_SimLagaoOffer_Charges = Jazz_SimLagaoOffer.getString("charges");

    } catch (JSONException e) {
        e.printStackTrace();
        Toast.makeText(getBaseContext(), JSONobj + "", Toast.LENGTH_SHORT).show();
    }

}
如何获取Json对象

这是我的Data.Json

{
“包”:{
“jazz_软件包”:{
“呼叫包”:{
“sim_lagao_优惠”:{
“费用”:“0.01”,
“检查代码”:“*551*2”,
“mbs”:“1500”,
“offnet”:“5000”,
“onnet”:“3000”,
“短信”:“3000”,
“子代码”:“*551”,
“不明嫌犯代码”:“*551*3”
}
}
}
}
}
试试这个

private void CopyAssets() {
    AssetManager assetManager = getAssets();
    String[] files = null;



        System.out.println("File name => "+filename);
        InputStream in = null;
        OutputStream out = null;
        try {
            in = assetManager.open(YOUR_ASSETS_FILE);   // if files resides inside the "Files" directory itself
            out = new FileOutputStream(STORAGE_PATH).toString() +"/" + filename);
            copyFile(in, out);
            in.close();
            in = null;
            out.flush();
            out.close();
            out = null;
        } catch(Exception e) {
           e.printStackTrace();
        }

}
private void copyFile(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    int read;
    while((read = in.read(buffer)) != -1){
        out.write(buffer, 0, read);
    }
}
使用以下代码从存储器中读取

    String jsongString = readFromFile();
    JSONObject mainJsonObject = new JSONObject(jsongString);
    JSONObject Jazz_SimLagaoOffer = mainJsonObject.getJSONObject("packages").getJSONObject("jazz_packages").getJSONObject("call_packages").getJSONObject("sim_lagao_offer");
使用下面的方法从内部存储文件读取数据并以字符串形式返回

private String readFromFile() {

String ret = "";
InputStream inputStream = null;
try {
    inputStream = openFileInput("names.json");

    if ( inputStream != null ) {
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String receiveString = "";
        StringBuilder stringBuilder = new StringBuilder();

        while ( (receiveString = bufferedReader.readLine()) != null ) {
            stringBuilder.append(receiveString);
        }

        ret = stringBuilder.toString();
    }
}
catch (FileNotFoundException e) {
    Log.e("login activity", "File not found: " + e.toString());
} catch (IOException e) {
    Log.e("login activity", "Can not read file: " + e.toString());
}
finally {
  try {
     inputStream.close();
  } catch (IOException e) {
    e.printStackTrace();
  }
}

return ret;
}


希望这项工作:)

经过一天的研究,感谢@pratik vekariya,我得到了自己问题的答案 帮了我很多忙

CopyAssets()工作完美,正如@pratik vekariya在其答案中所定义的那样,要阅读FromFile,请参阅我的问题loadJSONFromAssets() 我只是换了一条线

InputStream is = getAssets().open("Data.json");
用这个

InputStream is = new FileInputStream(getFilesDir().toString() +"/" + "Data.json");

要从文件中加载.json文件,并从inputStrem中获取json对象,这是正确的!但是如何在我从中加载时加载它呢assets@H2Five请检查我编辑的答案,添加了从存储加载数据的函数我需要加载json对象我编辑了我的问题请检查并指导我从复制的“data.json”获取json对象 file@H2Five你们能告诉我你们的json是怎样的,你们想从中得到哪一个对象吗?问题已经更新了