Immediately comes the object, I do not know the keys.

{ "Ключ": Значение, "Ключ2": Значение, "Ключ3": Значение } 

I do not understand how to parse it, help the slow-witted :) I need to derive both the key and the value.

  • What do you want to do? - titov_andrei
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

2 answers 2

JSON can contain values ​​and collections of values. The simplified parsing algorithm is as follows:

  1. Take the next item
  2. If it is not a collection, we print its name and value.
  3. If a collection, take its nested first element and repeat from step 1

JSON collection example:

 { "name" : "abc" , "email id " : ["abc@gmail.com","def@gmail.com","ghi@gmail.com"] } 

 JsonFactory factory = new JsonFactory(); ObjectMapper mapper = new ObjectMapper(factory); Map<String, Object> map = new HashMap<String, Object>(); // convert JSON string to Map map = mapper.readValue(json, new TypeReference<Map<String, String>>(){}); System.out.println(map); 

where json is a File , InputStream , Reader or String .

  • I have an ArrayList <HashMap <String, Object> list and I need to put these keys and values ​​in it in a loop with a tag binding, for example: hm = new HashMap <> (); hm.put (NAME, "KEY"); hm.put (VALUE, "VALUE"); list.add (hm); - expertgames
  • The last piece of code, after the words "Still possible," just for this. Only not in ArrayList<HashMap<String, Object>> , but in HashMap<String, Object> . Put in the map and then add your map to the List or do something else with them. PS Answer added a little. - Pavel Krizhanovskiy
  • Sorry, but I don’t understand how to do it ... In the end, I need this ArrayList <HashMap <String, Object >> to be inserted into the adapter and output it to the ListView. - expertgames
  • @expertgames, you have something extra piling up. Why go Map inside List in this task? I suppose that you need either a list of pairs (of objects like Map.Entry ), or you can get by with the usual Map . Revise your task in the direction of simplification. - iksuy
  • @iksuy no, the adapter really needs a map sheet. Only this is another topic, and in general the android tag. @expertgames you asked how to turn JSON into a map . You are given a complete, comprehensive answer. To learn how to pass JSON to the ListAdapter , create a new question. - Pavel Krizhanovskiy

Create a class with a key, write getters - setters. Put them in the fox and chase him through GSON. He will parse himself from parse it into json from json'a.