-1

i am trying to get data from api and save it in assets folder as a .json file so i can access it later.

the data i am getting from api is

[
 {
    "category" : "Top News",
    "data" : [
        {
            "title" : "Nashville Season 5 Premiere Set at CMT",
            "image_url" : "http://ia.media-imdb.com/images/M/MV5BMjA2NTE0NzkyMF5BMl5BanBnXkFtZTgwMjAwMzg5NjE@._V1._SY140_.jpg",
            "caption" : "The cancelled ABC country-music drama will make its CMT debut with a two-hour premiere on Thursday, January 5, at 9/8c, the cable network announced Wednesday",
            "time" : "1 hours ago"
        },
        {
            "title" : "Sarah Paulson Joins Ryan Murphy's FX Drama Feud as Geraldine Page",
            "image_url" : "http://ia.media-imdb.com/images/M/MV5BMTUzMTA3NjM4MV5BMl5BanBnXkFtZTcwNjk1NTAyMg@@._V1._SY140_.jpg",
            "caption" : "Ryan Murphy is taking a page from his successful playbook, casting frequent collaborator Sarah Paulson in his new FX anthology drama Feud.",
            "time" : "2 hours ago"
        },
        {
            "title" : "Ronald Reagan Biopic Draws ‘Soul Surfer’ Director Sean McNamara",
            "image_url" : "http://ia.media-imdb.com/images/M/MV5BMzEzOTk4OTQ2OF5BMl5BanBnXkFtZTYwMzkyODQ2._V1._SY140_.jpg",
            "caption" : "\"Soul Surfer\" Sean McNamara has signed to helm a Ronald Reagan biopic that’s set to start production next spring.",
            "time" : "3 hours ago"
        },
        {
            "title" : "Martin Lawrence Gets First Stand-Up Special in 14 Years at Showtime",
            "image_url" : "http://ia.media-imdb.com/images/M/MV5BMTczOTMwOTc1OF5BMl5BanBnXkFtZTgwNTc3MjY5NzE@._V1._SY140_.jpg",
            "caption" : "Martin Lawrence‘s first stand-up special in 14 years will air on Showtime next month, the network announced on Tuesday.",
            "time" : "10 hours ago"
        },
        {
            "title" : "Cmt Announces ‘Nashville’ Season 5 Premiere Date",
            "image_url" : "http://ia.media-imdb.com/images/M/MV5BMjgwMzg2ODgtZTkzYi00YTU0LThhYjMtMWM0Zjc0ZGFhMDViXkEyXkFqcGdeQXVyNjQxMDY5MjM@._V1._SY140_.jpg",
            "caption" : "\"Nashville\" is heading back to TV with a new network, new showrunners and a new year",
            "time" : "13 hours ago"
        }
    ]
}

i want to save this data in a .json file. how can I do this?? this is the result i want to create if their is any other way plz guide me i am doing this in card view in expandible list view.any help would be grateful

  • You can not write file in assets directory at run time. – Rutvik Bhatt Feb 15 '19 at 08:00
  • 1
    Possible duplicate of [ANDROID: How to save JSON data in a file and retrieve it?](https://stackoverflow.com/questions/40168601/android-how-to-save-json-data-in-a-file-and-retrieve-it) – insertusernamehere Feb 15 '19 at 08:00
  • is there any other way to do this..@RutvikBhatt. – Ahsan Mahmood Feb 15 '19 at 09:37
  • Is there a specific reason why you want to save it as a file? – rmanalo Feb 15 '19 at 09:52
  • no there is no reason and i donot know how to deals with this type of json,normally i have use simple ktype of json where there is one square bracket and all the data come in currely bracket.like this one [ { "order_no": "00003", "name": "Del Monte Pineapple Tidbits 567 gm", "featured_image": "http:\/\/digitalcodeeye.com\/24h\/public\/assets\/admin\/images\/ecommerce\/products\/5c583153182f6.jpeg", "product_amount": 209, "quantity": 1, "status": 2 } ] – Ahsan Mahmood Feb 15 '19 at 10:18

1 Answers1

0

you can use SharedPreferences simply.

for save :

SharedPreferences sp = getSharedPreferences("your_pref_key", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString("your_key", yourValue);
editor.commit();

for read:

SharedPreferences sp = getSharedPreferences("your_pref_key", Activity.MODE_PRIVATE);
 String yourJson = sp.getString("your_key","defaultValue");

if you want deserialization, use gson

Gson gson = new Gson();  
YourClass yourObject = gson.fromJson(yourJson, YourClass.class);
secret paladin
  • 222
  • 1
  • 8