Storing JSON object using Volley

2019-09-19 13:37发布

This is the structure of the JSON I need to Load,

   {
  "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=\"                

1条回答
▲ chillily
2楼-- · 2019-09-19 14:13

You could define a custom deserializer and register a type adapter with GSON. Also why are you using this:

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

.. when you intend to use GSON for deserialization? You could just do it in your deserializer.

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;
    }
}
查看更多
登录 后发表回答