0

I need some help in writing to an existing json file. I can parse the data and read from it using GSON or just json in this example. I did it this way to filter the results by id displayed on screen. So it grabs the add and searches my list of over 900 videos and then gives the ones selected. Only issue is i don't want to display them i want to save them :)

final Button button = findViewById(R.id.buttonx);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            // load json parse json and grab fields...
            // then write to new file!

            try {
                //Load File
                BufferedReader jsonReader = new BufferedReader(new InputStreamReader(getResources().openRawResource(R.raw.movielist)));
                StringBuilder jsonBuilder = new StringBuilder();
                for (String line = null; (line = jsonReader.readLine()) != null;) {
                    jsonBuilder.append(line).append("\n");
                }

                //Parse Json
                JSONTokener tokener = new JSONTokener(jsonBuilder.toString());
                JSONArray jsonArray = new JSONArray(tokener);

                ArrayList<String> fields = new ArrayList<String>();
                for (int index = 0; index < jsonArray.length(); index++) {
                    //Set both values into the listview
                    JSONObject jsonObject = jsonArray.getJSONObject(index);
                    String series = jsonObject.getString("id");

                    if (series.equals(tvId.getText())) {
                        fields.add(jsonObject.getString("movie"));
                        fields.add(jsonObject.getString("year"));
                        fields.add(jsonObject.getString("duration"));
                        fields.add(jsonObject.getString("director"));
                        fields.add(jsonObject.getString("image"));
                        fields.add(jsonObject.getString("id"));
                    }

                }

            } catch (FileNotFoundException e) {
                Log.e("jsonFile", "file not found");
            } catch (IOException e) {
                Log.e("jsonFile", "ioerror");
            } catch (JSONException e) {
                Log.e("jsonFile", "error while parsing json");
            }


            Toast.makeText(DetailActivity.this, "The following movie has been saved " + tvId.getText(), Toast.LENGTH_LONG).show();

            // Toast.makeText(DetailActivity.this, "This features is not working", Toast.LENGTH_LONG).show();

        }
    });

My issue is once I get the data I want to be able to write this to an existing JSON file. my api is 18 so can't use FILEwriter i am trying to make the app available to pretty much everyone. A point in the right direction would be great.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Ghost.Idle
  • 69
  • 8
  • I was using setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, fields)); to display the results. – Ghost.Idle Apr 13 '18 at 10:33
  • So what exactly? You want to save all video details in your app. Don't want to show on listView? am I correct? If this is the case, why not using database? – Danger Apr 13 '18 at 10:37
  • My whole app relies and uses JSON 800 results all linked up for online image sharing. All images are cached when user calls them if user requires them. What I want to do is: User clicks on a button the id of the movie is "Grabbed" then searched through the JSON file. When it is found it grabs the whole json objects the ones I have listed above. Then I want that information to be saved on another JSON file. So the user can open that file and have a list of what they have saved. – Ghost.Idle Apr 13 '18 at 10:49
  • Maybe something like this: https://stackoverflow.com/questions/44416855/adding-json-objects-to-existing-json-file . After that you can convert map to json: https://stackoverflow.com/questions/15402321/how-to-convert-hashmap-to-json-array-in-android. Sorry if I was not helpful. Best of luck. – U.Mitic Apr 13 '18 at 11:21
  • Thanks for the advice U.Mitic but due to the API being 18 i can't use FileWriter i think it will be most suitable to create a sql database based off of the json data. Then have a variable that is true or false then just edit that field when called and list the true fields. Just need to work that one out now used sql before but never on an android device. this will be fun. – Ghost.Idle Apr 15 '18 at 09:48
  • For anyone wanting to do this, I just used SQL Lite. Grabbed the data shown saved to the table and then just called the data from the SQL table when needed. – Ghost.Idle Apr 28 '18 at 10:36

1 Answers1

0

If you want to write to a file packaged in the raw folder, you will have to copy it to the local file system first. Resources contained in your raw directory in your project will be packaged inside your APK and will not be writeable at runtime. Consider looking at the Internal or External Data Storage APIs. Update raw resources is not possible

Prashant Jajal
  • 3,469
  • 5
  • 24
  • 38
  • Yes this is how I load up my JSON files. It allows the user to not be connected to the internet. Seems a bit silly that you can't edit them whilst the app is running. Because all it is a simple copy and paste. What I want to do is grab the object from the raw folder and add a new line in another json. I guess ill dig iron with file writers and json writer. – Ghost.Idle Apr 13 '18 at 19:18
  • Ok how about this. Boot up the app as usual head over to save movie. Click button it saves an "id" now this id is stored as a list in a text file locally. Right, new id new line endless. Now lets say if i was to do a json search using that data? New line grab ID search display results move onto next search display etc etc... Then im not editing any json just saving data to a text file that is being read from. Long shout but would this work instead? – Ghost.Idle Apr 13 '18 at 20:06