简体   繁体   English

从 api 获取数据并将其保存在文件中

[英]Get Data From api and save it in a file

i am trying to get data from api and save it in assets folder as a .json file so i can access it later.我正在尝试从 api 获取数据并将其作为 .json 文件保存在资产文件夹中,以便我以后可以访问它。

the data i am getting from api is我从 api 得到的数据是

[
 {
    "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.我想将此数据保存在 .json 文件中。 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 use SharedPreferences simply.您可以简单地使用 SharedPreferences。

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 gson = new Gson();  
YourClass yourObject = gson.fromJson(yourJson, YourClass.class);

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

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