有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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

我用这个来加载数据。来自资产的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对象

这是我的数据。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#" } } } } }

共 (2) 个答案

  1. # 1 楼答案

    试试这个

    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;
    

    }

    希望这项工作:)

  2. # 2 楼答案

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

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

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

    用这个

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

    装载。从文件中获取json文件,并从inputStrem获取json对象