繁体   English   中英

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

[英]How to Save "Data.Json" file from assets to internal Storage and then use it for read/write

目前,我正在从资产的 Json 文件“ Data.json ”中获取包详细信息(Onnet Minutes、Offnet Minutes 等),但我知道我们无法更改资产的值。 所以我的问题是如何将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

 { "packages" : { "jazz_packages" : { "call_packages" : { "sim_lagao_offer" : { "charges" : "0.01", "check_code" : "*551*2#", "mbs" : "1500", "offnet" : "5000", "onnet" : "3000", "sms" : "3000", "sub_code" : "*551#", "unsub_code" : "*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;

}

希望这项工作:)

经过 1 天的研究,我回答了我自己的问题,感谢 @pratik vekariya 对我的帮助很大。

CopyAssets() 按照@pratik vekariya 在他的回答中的定义完美运行,并从文件中读取,请参阅我的问题 loadJSONFromAssets() 和我刚刚替换了行

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

有了这个

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

文件加载 .json 文件并从 inputStrem 获取 json 对象

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM