Skip to content
Advertisement

JSON object from java ObjectName.toString

I have a java object witch generates this output when i type objectName.toString() :

Bus [id=1, nextStationID=0, previousStationID=0]

Is there a JSON parser that lets me make a JSON from the string that my object generates? something like: JsonObject x = new JsonObject(busObject.toString())

It is important for me to generate it from the string that i get when calling the .toString method.

Any help is appreciated.

Advertisement

Answer

I have fixed my problem by overriding my toString() and printing directly a json object

StringBuilder builder = new StringBuilder();
            builder.append("{"id" :");
            builder.append(id);
            builder.append(", "latitude" :");
            builder.append(latitude);
            builder.append(", "longitude" :");
            builder.append(longitude);
            builder.append("}");
        return builder.toString();
4 People found this is helpful
Advertisement