ListObject to String in Java

3

I have a List of an Object.

public class BusStop {

    private String description;

    private float lat;

    private float lng;

    //getter and setter

}

Of which I try to put all its content inside a variable of type String.

I tried this:

StringBuilder aux = new StringBuilder();
busStop.forEach(aux::append);

return aux.toString();

But the answer I get is not what I want.

  

en.yo.app.model.BusStop @ 6cdf7f4bes.yo.app.model.BusStop @ 15820f4bes.yo.app.model.BusStop @ 2022ad11

And this is just what I want to achieve

  

[{"lat": 43.482243622643026, "lng": - 3.7942432892720035, "description": "1"}, {"lat": 43.47775946266253, "lng": - 3.8052296173970035, "description": "2"}, {"lat": 43.474520695703184, "lng": - 3.822567416469269, "description": "3"}]

    
asked by Eduardo 10.05.2018 в 12:06
source

5 answers

2

The problem is that you are calling the toString () method and you do not have it overwritten. If the toString () method is not overwritten, it is called the parent's (in this case, Object's, the parent of all) and what it does is show the representation of the class and the object. Here's the documentation about it

You should create the overwriting more or less like this

@Override
public String toString(){
 return "{ lat:" + lat + (otras propiedades) +"}"
}
    
answered by 10.05.2018 / 12:13
source
2

One option is to use Streams and map() :

public List<String> getStringList(List<BusStop> busStopList){
    return busStopList.stream()
           .map(item->getItemAsString(item))
           .collect(Collectors.toList());
}
private String getItemAsString(BusStop busStop){
    StringBuilder text = new StringBuilder();
    text.append("{");
    text.append("lat=").append(lat).append(",");
    text.append("lng=").append(lng).append(",");
    text.append("description=").append(description);
    text.append("}");
    return text.toString();
}
    
answered by 10.05.2018 в 12:25
1

You must either override the toString () method of the BusStop class or define a specific one that will return the JSON representation of that instance and invoke it in that forEach ().

Example:

public String generaJSON() {
    String patron = "{\"lat\":%s, \"lng\":%s, \"description\":\"%s\"}";
    return String.format(patron, getLat(), getLng(), getDescription());
}

Another option would be that you look for libraries like FasterXML Jackson to convert instances of objects to JSON string and vice versa

    
answered by 10.05.2018 в 12:14
1

You can always overwrite the toString () method of the Class, so it could be something like:

@Override
public String toString() { 
     return "{'lat':" + lat + ",'lng':" + lng + ",'description':'" + description + "'}";
}
    
answered by 10.05.2018 в 12:14
0

Try this method in class BusStop .

public Staff crearJsonFromObject(BusStop obj) {

        Staff staff = new Staff();

        staff.setLat(obj.getLat());
        staff.setLng(obj.getLng());
        staff.setDescription(obj.getDescription());

        return staff;

    }

Exit:

{"lat":3423434,"lng":23424242,"description":"wefwefwf"}
    
answered by 10.05.2018 в 12:24