1

I'm getting a list of values from a query with MongoDB I'm using MongoRepository and the method findAll, the answer I'm getting is a List of Informacion

public Informacion[] getAll()  {
    List<Informacion> info = repoInfo.findAll();
    String json =new Gson().toJson(info);
    Informacion[] array = info.toArray(new Informacion[info.size()]);
    return array;
}

the list I'm getting I parse into JSON, but I can't convert it into a JSONObject to work properly with all the values inside, this is the JSON string :

Informacion{ preferencias=[Preferencias(nombrePref=Rock),
Preferencias(nombrePref=Tatuajes)], numTelefono='0984623854', 
usuario='@Bryan810', redes=[RedesSociales(nombreRedSocial=Twitter)], 
fechaRecarga=Fri Dec 21 11:30:59 COT 2018}

working with this as a String is really hard because values like Preferencias has many values but if I can transform the String to JSONObject I think I can't handle it better.

So my question is how can I transform "JSON" String to a JSONObject?

Jacob
  • 586
  • 6
  • 27
Bryan Saltos
  • 63
  • 2
  • 8
  • You have a List So use that list of objects. Why do you transform it to a JSON string in the first place (and to an array)? – JB Nizet Dec 27 '18 at 07:17
  • You are converting it to string yourself, why don't you use `List info` itself? – Aditya Narayan Dixit Dec 27 '18 at 07:23
  • Related Question https://stackoverflow.com/questions/5245840/how-to-convert-jsonstring-to-jsonobject-in-java – Kapil Dec 27 '18 at 07:31
  • when i use List i have this answer: Informacion{ preferencias=[Preferencias(nombrePref=Rock), Preferencias(nombrePref=Tatuajes)], ..., redes= RedesSociales(nombreRedSocial=Twitter)],..} the problem is that i dont know how to handle values like preferencias because if i use get method to this i get this : Preferencias(nombrePref=Tatuajes) and i only want the value "Tatuajes" im new with mongodb and this value "prefrencias" is declared as a list in the POJO thats why im trying to convert it into JsonObject because i have the idea that i can handle it better – Bryan Saltos Dec 27 '18 at 16:26

0 Answers0