1

I was looking up the JsonObject class in Java: http://docs.oracle.com/javaee/7/api/javax/json/JsonObject.html

I was trying to figure out how to take an object which I created and return it as a string. See below:

public String toJson ()
  {
  JsonObject data = Json.createObjectBuilder();
  //this._myMap is just the private map variable defined: <String, FooClass>
  for ( Entry<String, FooClass> tuple : this._myMap.entrySet() )
    {
    data.add(type.getKey(), tuple.getValue().toString());
    }
  data.build();
  // String out = ...?
  return "-1";
  }

after first glance of the documentation, there isnt a toStyledString, or anything which would return a string, so i was curious as to how to approach it. Im thinking that i could just do something with Json.createWriter or equivalent, but was not sure.

Fallenreaper
  • 10,222
  • 12
  • 66
  • 129

1 Answers1

2

It looks like toString was overriden, and when looking at the defintion of that function implementation, it states at: http://docs.oracle.com/javaee/7/api/javax/json/JsonValue.html#toString()

toString

String toString()

Returns JSON text for this JSON value.

Overrides: toString in class Object

Returns: JSON text

Community
  • 1
  • 1
Fallenreaper
  • 10,222
  • 12
  • 66
  • 129