简体   繁体   English

使用Volley存储JSON对象

[英]Storing JSON object using Volley

This is the structure of the JSON I need to Load, 这是我需要加载的JSON的结构,

   {
  "readme_0" : "THIS JSON IS THE RESULT OF YOUR SEARCH QUERY - THERE IS NO WEB PAGE WHICH SHOWS THE RESULT!",
  "readme_1" : "loklak.org is the framework for a message search system, not the portal, read: http://loklak.org/about.html#notasearchportal",
  "readme_2" : "This is supposed to be the back-end of a search portal. For the api, see http://loklak.org/api.html",
  "readme_3" : "Parameters q=(query), source=(cache|backend|twitter|all), callback=p for jsonp, maximumRecords=(message count), minified=(true|false)",
  "search_metadata" : {
    "itemsPerPage" : "100",
    "count" : "100",
    "count_twitter_all" : 0,
    "count_twitter_new" : 100,
    "count_backend" : 0,
    "count_cache" : 78780,
    "hits" : 78780,
    "period" : 3066,
    "query" : "apple",
    "client" : "180.215.121.78",
    "time" : 5219,
    "servicereduction" : "false",
    "scraperInfo" : "http://45.55.245.191:9000,local"
  },
  "statuses" : [ {
    "created_at" : "2016-01-09T12:11:38.000Z",
    "screen_name" : "arifazmi92",
    "text" : "Perhaps I shouldn't have eaten that pisang goreng cheese perisa green apple. <img class=\"Emoji Emoji--forText\" src=\"https://abs.twimg.com/emoji/v2/72x72/1f605.png\" draggable=\"false\" alt=\"😅\" title=\"Smiling face with open mouth and cold sweat\" aria-label=\"Emoji: Smiling face with open mouth and cold sweat\"><img class=\"Emoji Emoji--forText\" src=\"https://abs.twimg.com/emoji/v2/72x72/1f605.png\" draggable=\"false\" alt=\"😅\" title=\"Smiling face with open mouth and cold sweat\" aria-label=\"Emoji: Smiling face with open mouth and cold sweat\"><img class=\"Emoji Emoji--forText\" src=\"https://abs.twimg.com/emoji/v2/72x72/1f605.png\" draggable=\"false\" alt=\"😅\" title=\"Smiling face with open mouth and cold sweat\" aria-label=\"Emoji: Smiling face with open mouth and cold sweat\">",
    "link" : "https://twitter.com/arifazmi92/status/685796067082813440",
    "id_str" : "685796067082813440",
    "source_type" : "TWITTER",
    "provider_type" : "SCRAPED",
    "retweet_count" : 0,
    "favourites_count" : 0,
    "images" : [ ],
    "images_count" : 0,
    "audio" : [ ],
    "audio_count" : 0,
    "videos" : [ ],
    "videos_count" : 0,
    "place_name" : "Bandar Shah Alam, Selangor",
    "place_id" : "9be3b0eca6c21f6c",
    "place_context" : "FROM",
    "place_country" : "Malaysia",
    "place_country_code" : "MY",
    "place_country_center" : [ -59.30559537806809, 3.4418498787292435 ],
    "location_point" : [ 101.53280621465888, 3.0850698533863863 ],
    "location_radius" : 0,
    "location_mark" : [ 101.52542227271437, 3.0911033774188725 ],
    "location_source" : "PLACE",
    "hosts" : [ "abs.twimg.com" ],
    "hosts_count" : 1,
    "links" : [ "https://abs.twimg.com/emoji/v2/72x72/1f605.png\"", "https://abs.twimg.com/emoji/v2/72x72/1f605.png\"", "https://abs.twimg.com/emoji/v2/72x72/1f605.png\"" ],
    "links_count" : 3,
    "mentions" : [ ],
    "mentions_count" : 0,
    "hashtags" : [ ],
    "hashtags_count" : 0,
    "without_l_len" : 626,
    "without_lu_len" : 626,
    "without_luh_len" : 626,
    "user" : {
      "screen_name" : "arifazmi92",
      "user_id" : "44503967",
      "name" : "Arif Azmi",
      "profile_image_url_https" : "https://pbs.twimg.com/profile_images/685788990004301824/NbFnnLuO_bigger.jpg",
      "appearance_first" : "2016-01-09T12:11:57.933Z",
      "appearance_latest" : "2016-01-09T12:11:57.933Z"
    }
  }
  } ],
  "aggregations" : { }
}

And these are my POJO classes that I've generated: 这些是我生成的POJO类:

MainPojo.class MainPojo.class

    public class MainPojo
{
    @SerializedName("readme_0")
    @Expose
    private String readme0;
    @SerializedName("readme_1")
    @Expose
    private String readme1;
    @SerializedName("readme_2")
    @Expose
    private String readme2;
    @SerializedName("readme_3")
    @Expose
    private String readme3;
    @SerializedName("search_metadata")
    @Expose
    private SearchMetadata searchMetadata;
    @SerializedName("statuses")
    @Expose
    private List<Status> statuses = new ArrayList<Status>();
    @SerializedName("aggregations")
    @Expose
    private Aggregations aggregations;


    public String getReadme0() {
        return readme0;
    }

    public void setReadme0(String readme0) {
        this.readme0 = readme0;
    }


    public String getReadme1() {
        return readme1;
    }


    public void setReadme1(String readme1) {
        this.readme1 = readme1;
    }


    public String getReadme2() {
        return readme2;
    }

    public void setReadme2(String readme2) {
        this.readme2 = readme2;
    }


    public String getReadme3() {
        return readme3;
    }


    public void setReadme3(String readme3) {
        this.readme3 = readme3;
    }

    public SearchMetadata getSearchMetadata() {
        return searchMetadata;
    }


    public void setSearchMetadata(SearchMetadata searchMetadata) {
        this.searchMetadata = searchMetadata;
    }


    public List<Status> getStatuses() {
        return statuses;
    }


    public void setStatuses(List<Status> statuses) {
        this.statuses = statuses;
    }


    public Aggregations getAggregations() {
        return aggregations;
    }


    public void setAggregations(Aggregations aggregations) {
        this.aggregations = aggregations;
    }

}

Status.class 状态类

    public class Status
{

    @SerializedName("created_at")
    @Expose
    private String createdAt;
    @SerializedName("screen_name")
    @Expose
    private String screenName;
    @SerializedName("text")
    @Expose
    private String text;
    @SerializedName("link")
    @Expose
    private String link;
    @SerializedName("id_str")
    @Expose
    private String idStr;
    @SerializedName("source_type")
    @Expose
    private String sourceType;
    @SerializedName("provider_type")
    @Expose
    private String providerType;
    @SerializedName("retweet_count")
    @Expose
    private Integer retweetCount;
    @SerializedName("favourites_count")
    @Expose
    private Integer favouritesCount;
    @SerializedName("images")
    @Expose
    private List<Object> images = new ArrayList<Object>();
    @SerializedName("images_count")
    @Expose
    private Integer imagesCount;
    @SerializedName("audio")
    @Expose
    private List<Object> audio = new ArrayList<Object>();
    @SerializedName("audio_count")
    @Expose
    private Integer audioCount;
    @SerializedName("videos")
    @Expose
    private List<Object> videos = new ArrayList<Object>();
    @SerializedName("videos_count")
    @Expose
    private Integer videosCount;
    @SerializedName("place_name")
    @Expose
    private String placeName;
    @SerializedName("place_id")
    @Expose
    private String placeId;
    @SerializedName("place_context")
    @Expose
    private String placeContext;
    @SerializedName("location_point")
    @Expose
    private List<Double> locationPoint = new ArrayList<Double>();
    @SerializedName("location_radius")
    @Expose
    private Integer locationRadius;
    @SerializedName("location_mark")
    @Expose
    private List<Double> locationMark = new ArrayList<Double>();
    @SerializedName("location_source")
    @Expose
    private String locationSource;
    @SerializedName("hosts")
    @Expose
    private List<String> hosts = new ArrayList<String>();
    @SerializedName("hosts_count")
    @Expose
    private Integer hostsCount;
    @SerializedName("links")
    @Expose
    private List<String> links = new ArrayList<String>();
    @SerializedName("links_count")
    @Expose
    private Integer linksCount;
    @SerializedName("mentions")
    @Expose
    private List<Object> mentions = new ArrayList<Object>();
    @SerializedName("mentions_count")
    @Expose
    private Integer mentionsCount;
    @SerializedName("hashtags")
    @Expose
    private List<Object> hashtags = new ArrayList<Object>();
    @SerializedName("hashtags_count")
    @Expose
    private Integer hashtagsCount;
    @SerializedName("without_l_len")
    @Expose
    private Integer withoutLLen;
    @SerializedName("without_lu_len")
    @Expose
    private Integer withoutLuLen;
    @SerializedName("without_luh_len")
    @Expose
    private Integer withoutLuhLen;
    @SerializedName("user")
    @Expose
    private User user;
    @SerializedName("provider_hash")
    @Expose
    private String providerHash;
    @SerializedName("classifier_language")
    @Expose
    private String classifierLanguage;
    @SerializedName("classifier_language_probability")
    @Expose
    private Double classifierLanguageProbability;


    public String getCreatedAt() {
        return createdAt;
    }


    public void setCreatedAt(String createdAt) {
        this.createdAt = createdAt;
    }


    public String getScreenName() {
        return screenName;
    }


    public void setScreenName(String screenName) {
        this.screenName = screenName;
    }


    public String getText() {
        return text;
    }


    public void setText(String text) {
        this.text = text;
    }


    public String getLink() {
        return link;
    }


    public void setLink(String link) {
        this.link = link;
    }


    public String getIdStr() {
        return idStr;
    }


    public void setIdStr(String idStr) {
        this.idStr = idStr;
    }


    public String getSourceType() {
        return sourceType;
    }


    public void setSourceType(String sourceType) {
        this.sourceType = sourceType;
    }


    public String getProviderType() {
        return providerType;
    }


    public void setProviderType(String providerType) {
        this.providerType = providerType;
    }


    public Integer getRetweetCount() {
        return retweetCount;
    }


    public void setRetweetCount(Integer retweetCount) {
        this.retweetCount = retweetCount;
    }


    public Integer getFavouritesCount() {
        return favouritesCount;
    }


    public void setFavouritesCount(Integer favouritesCount) {
        this.favouritesCount = favouritesCount;
    }

    public List<Object> getImages() {
        return images;
    }

    public void setImages(List<Object> images) {
        this.images = images;
    }

    public Integer getImagesCount() {
        return imagesCount;
    }


    public void setImagesCount(Integer imagesCount) {
        this.imagesCount = imagesCount;
    }


    public List<Object> getAudio() {
        return audio;
    }


    public void setAudio(List<Object> audio) {
        this.audio = audio;
    }


    public Integer getAudioCount() {
        return audioCount;
    }


    public void setAudioCount(Integer audioCount) {
        this.audioCount = audioCount;
    }


    public List<Object> getVideos() {
        return videos;
    }


    public void setVideos(List<Object> videos) {
        this.videos = videos;
    }


    public Integer getVideosCount() {
        return videosCount;
    }


    public void setVideosCount(Integer videosCount) {
        this.videosCount = videosCount;
    }


    public String getPlaceName() {
        return placeName;
    }

    public void setPlaceName(String placeName) {
        this.placeName = placeName;
    }


    public String getPlaceId() {
        return placeId;
    }


    public void setPlaceId(String placeId) {
        this.placeId = placeId;
    }


    public String getPlaceContext() {
        return placeContext;
    }


    public void setPlaceContext(String placeContext) {
        this.placeContext = placeContext;
    }


    public List<Double> getLocationPoint() {
        return locationPoint;
    }


    public void setLocationPoint(List<Double> locationPoint) {
        this.locationPoint = locationPoint;
    }


    public Integer getLocationRadius() {
        return locationRadius;
    }


    public void setLocationRadius(Integer locationRadius) {
        this.locationRadius = locationRadius;
    }


    public List<Double> getLocationMark() {
        return locationMark;
    }


    public void setLocationMark(List<Double> locationMark) {
        this.locationMark = locationMark;
    }


    public String getLocationSource() {
        return locationSource;
    }


    public void setLocationSource(String locationSource) {
        this.locationSource = locationSource;
    }

    public List<String> getHosts() {
        return hosts;
    }


    public void setHosts(List<String> hosts) {
        this.hosts = hosts;
    }

    public Integer getHostsCount() {
        return hostsCount;
    }


    public void setHostsCount(Integer hostsCount) {
        this.hostsCount = hostsCount;
    }


    public List<String> getLinks() {
        return links;
    }

    public void setLinks(List<String> links) {
        this.links = links;
    }


    public Integer getLinksCount() {
        return linksCount;
    }


    public void setLinksCount(Integer linksCount) {
        this.linksCount = linksCount;
    }


    public List<Object> getMentions() {
        return mentions;
    }


    public void setMentions(List<Object> mentions) {
        this.mentions = mentions;
    }


    public Integer getMentionsCount() {
        return mentionsCount;
    }


    public void setMentionsCount(Integer mentionsCount) {
        this.mentionsCount = mentionsCount;
    }


    public List<Object> getHashtags() {
        return hashtags;
    }

    public void setHashtags(List<Object> hashtags) {
        this.hashtags = hashtags;
    }


    public Integer getHashtagsCount() {
        return hashtagsCount;
    }


    public void setHashtagsCount(Integer hashtagsCount) {
        this.hashtagsCount = hashtagsCount;
    }


    public Integer getWithoutLLen() {
        return withoutLLen;
    }


    public void setWithoutLLen(Integer withoutLLen) {
        this.withoutLLen = withoutLLen;
    }


    public Integer getWithoutLuLen() {
        return withoutLuLen;
    }


    public void setWithoutLuLen(Integer withoutLuLen) {
        this.withoutLuLen = withoutLuLen;
    }


    public Integer getWithoutLuhLen() {
        return withoutLuhLen;
    }

    public void setWithoutLuhLen(Integer withoutLuhLen) {
        this.withoutLuhLen = withoutLuhLen;
    }


    public User getUser() {
        return user;
    }


    public void setUser(User user) {
        this.user = user;
    }


    public String getProviderHash() {
        return providerHash;
    }


    public void setProviderHash(String providerHash) {
        this.providerHash = providerHash;
    }


    public String getClassifierLanguage() {
        return classifierLanguage;
    }


    public void setClassifierLanguage(String classifierLanguage) {
        this.classifierLanguage = classifierLanguage;
    }


    public Double getClassifierLanguageProbability() {
        return classifierLanguageProbability;
    }


    public void setClassifierLanguageProbability(Double classifierLanguageProbability) {
        this.classifierLanguageProbability = classifierLanguageProbability;
    }

}

User.java User.java

    public class User {

    @SerializedName("screen_name")
    @Expose
    private String screenName;
    @SerializedName("user_id")
    @Expose
    private String userId;
    @SerializedName("name")
    @Expose
    private String name;
    @SerializedName("profile_image_url_https")
    @Expose
    private String profileImageUrlHttps;
    @SerializedName("appearance_first")
    @Expose
    private String appearanceFirst;
    @SerializedName("appearance_latest")
    @Expose
    private String appearanceLatest;

    public String getScreenName() {
        return screenName;
    }


    public void setScreenName(String screenName) {
        this.screenName = screenName;
    }


    public String getUserId() {
        return userId;
    }


    public void setUserId(String userId) {
        this.userId = userId;
    }


    public String getName() {
        return name;
    }


    public void setName(String name) {
        this.name = name;
    }


    public String getProfileImageUrlHttps() {
        return profileImageUrlHttps;
    }


    public void setProfileImageUrlHttps(String profileImageUrlHttps) {
        this.profileImageUrlHttps = profileImageUrlHttps;
    }


    public String getAppearanceFirst() {
        return appearanceFirst;
    }


    public void setAppearanceFirst(String appearanceFirst) {
        this.appearanceFirst = appearanceFirst;
    }


    public String getAppearanceLatest() {
        return appearanceLatest;
    }


    public void setAppearanceLatest(String appearanceLatest) {
        this.appearanceLatest = appearanceLatest;
    }
}

Aggregations.class 聚合类

    public class Aggregations {
}

And finally , this is the code I use to read the JSON and store as JSON objects, 最后 ,这是我用来读取JSON并存储为JSON对象的代码,

 SharedPreferences Tempx = getSharedPreferences("ActivitySession", Context.MODE_PRIVATE);
                    SharedPreferences.Editor edx = Tempx.edit();
                    edx.putString("GSON_FEED", response.toString());
                    edx.apply();

                    Gson gson = new Gson();
                    JsonParser parser = new JsonParser();



                    try{
                        JsonArray jArray = parser.parse(Tempx.getString("GSON_FEED","")).getAsJsonArray();
                        for(JsonElement obj : jArray )
                        {
                            MainPojo cse = gson.fromJson( obj , MainPojo.class);
                            TweetList.add(cse);
                        }
                    }catch(Throwable e) {

                        JsonElement obj = parser.parse(Tempx.getString("GSON_FEED","")).getAsJsonObject();
                        MainPojo cse = gson.fromJson( obj , MainPojo.class);
                        TweetList.add(cse);

                    }

Though I am able to log the JSON as String, I don't know if I am storing it the wrong way, any help will be much appreciated, Thanks! 尽管我可以将JSON记录为String,但我不知道是否以错误的方式存储它,任何帮助将不胜感激,谢谢!

You could define a custom deserializer and register a type adapter with GSON. 您可以定义一个自定义反序列化器,并向GSON注册一个类型适配器。 Also why are you using this: 另外,为什么要使用这个:

JsonArray jArray = parser.parse(Tempx.getString("GSON_FEED","")).getAsJsonArray();

.. when you intend to use GSON for deserialization? ..您打算何时使用GSON进行反序列化? You could just do it in your deserializer. 您可以在反序列化器中执行此操作。

https://sites.google.com/site/gson/gson-user-guide#TOC-Custom-Serialization-and-Deserialization https://sites.google.com/site/gson/gson-user-guide#TOC-Custom-Serialization-and-Deserialization

EG: 例如:

public class FooDeserializer implements JsonDeserializer<Foos>
{
    @Override public Foos deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
    {
        JsonObject jsonObject = json.getAsJsonObject();
        JsonArray statusArray = jsonObject.get("statuses").getAsJsonArray();
        Foos result = new Foos();
        ArrayList fooArray = new ArrayList<>;
        for (JsonElement e : statusArray) {
            fooArray.add(new Foo());
        }
        result.setFoos(fooArray);
        return result;
    }
}

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

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