Tell me, please, how best to add data from JSON to ComboBox. The graphic part is made using Scene Builder, there is a class Controls, where all the id are registered. I give an example of a parser

public class Parser { private String filePath = "/Users/oleg/Documents/workspace/app/src/basebase.json"; public List<JSONObject> ParseJson() { List<JSONObject> resultList = new ArrayList<JSONObject>(); try { // read the json file FileReader reader = new FileReader(filePath); JSONParser jsonParser = new JSONParser(); JSONObject jsonObject = (JSONObject) jsonParser.parse(reader); JSONObject first = (JSONObject) jsonObject.get("Sample"); resultList.add(first); } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } catch (ParseException ex) { ex.printStackTrace(); } catch (NullPointerException ex) { ex.printStackTrace(); } return resultList; 

I also demonstrate the structure of JSON

 { “Sample”: { “Text1”: 001, “Text2”: 002 }, “Sample2”: { “Text1”: 001, “Text2”: 002 } } 
  • What exactly should get into the combobox and in what format? - Mikhail Vaysman
  • Sample, Sample2 and so on should go into the combobox. - Oleg Mavromatti

1 answer 1

Suppose you have strings in JSON

  // json - переменная в которой содержится JSON в виде строки String json = ...; // decodeJSON - метод который вы напишите, что бы получить список с строк из JSON List<String> newItems = decodeJSON(json); ObservableList<String> items = combobox.getItems(); items.addAll(newItems); 
  • Thanks for the quick response. As far as I understood, should the parsing occur in the decodeJSON method? - Oleg Mavromatti
  • Yes. There it is necessary to disassemble json. - Mikhail Vaysman
  • Then what is here (decodeJSON (json)) is json in brackets? - Oleg Mavromatti
  • I updated the answer - Mikhail Vaysman
  • show the structure of your JSON in question - Mikhail Vaysman