#json #formatting
Вопрос:
Мне нужно вывести этот список записей с новой строкой для каждой записи в порядке возрастания по идентификатору и пропустить записи без данных о массе или годе. Как создать новую строку для каждой записи, расположить их по порядку и пропустить записи без определенных данных. Вот код для настройки набора данных:
class Geolocation{ String type; Listlt;Floatgt; coordinates; public Geolocation(String type, Listlt;Floatgt; coordinates){ this.type = type; this.coordinates = this.coordinates; } } class Record{ String name; int id; String nametype; String recclass; float mass; String fall; String year; Geolocation geolocation; public Record(String name, int id, String nametype, String recclass, float mass, String fall, String year, Geolocation geolocation){ this.name = name; this.id = id; this.nametype = nametype; this.recclass = recclass; this.mass = mass; this.fall = fall; this.year = year; this.geolocation = geolocation; } } public class Solution { /* The method below reads the input JSON string and returns a list of Record objects as output. */ public static Listlt;Recordgt; fetchData(){ Listlt;Stringgt; tokens = new ArrayListlt;gt;(); Listlt;Recordgt; recordList = new ArrayListlt;gt;(); Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { tokens.add(scanner.next()); } String inputJSON = String.join(" ", tokens); JsonArray dataArray = new JsonParser().parse(inputJSON).getAsJsonArray(); Gson gson = new Gson(); for(int i = 0; i lt; dataArray.size(); i ){ JsonObject record_obj = dataArray.get(i).getAsJsonObject(); Record record = gson.fromJson(record_obj, Record.class); recordList.add(record); } scanner.close(); return recordList; } public static void main(String args[] ) throws Exception { Listlt;Recordgt; recordList = fetchData(); // Use this list of Record objects to complete the solution
Комментарии:
1. Какова цель
String inputJSON = String.join(" ", tokens);
этого ? Можете ли вы привести пример добавленного элементаtokens
?