Quantcast
Viewing all articles
Browse latest Browse all 3

Answer by SHartley for Sorting a JSON object in Android studios (java)

I'm only familiar with using Google's gson library which is a bit old. If there are better json parsers available I'm sure somebody can chime in.You'd use it like this to extract the information:

String restaurantName;JsonElement restaurantJSONElement;JsonPrimitive restaurantJSONPrimitive;JsonParser parser = new JsonParser();// myJSONstring is the raw JSON dataJsonElement parentJSONElement = parser.parse(myJSONstring);JsonObject parentJSONObject = parentJSONElement.getAsJsonObject();JsonArray restaurantsJSONArray = (JsonArray) parentJSONObject.get("restaurants");for(int i=0;i<restaurantsJSONArray.size();i++){  //get the element  restaurantJSONElement = ((JsonObject)restaurantsJSONArray.get(i)).get("restaurant");  //get the restaurant name  restaurantJSONPrimitive = ((JsonObject)restaurantJSONElement.getAsJsonObject()).getAsJsonPrimitive("name");  restaurantName = restaurantJSONPrimitive.getAsString();}

You could extract all the members and do whatever you want with them, put them in custom object arrays and sort them by restaurantName as the comparitor


Viewing all articles
Browse latest Browse all 3

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>