How can I check that the JSONObject object does not contain any embedded JSON objects?
That is, an object of the form
{ "first" : "1", "second" : "2" } must be valid and the view object
{ "first" : "1", "inner" : { "inner-field" : "data" } } must be invalid.
My attempt to follow one of the tips:
package json; import com.google.gson.JsonObject; import org.junit.Test; import com.google.gson.JsonParser; import com.google.gson.JsonElement; public class TestNested { @Test public void testHasNestedJSON() { final String nested = "{ \"data\" : \"test\", \"inner\" : { \"key\" : \"inn\"} }"; System.out.println(nested); JsonParser parser = new JsonParser(); System.out.println(parser.parse(nested).getClass()); } @Test public void testPlainJSON() { final String plain = "{ \"data\" : \"test\" }"; System.out.println(plain); JsonParser parser = new JsonParser(); JsonElement jsonElement = parser.parse(plain); JsonObject jsonObject = jsonElement.getAsJsonObject(); System.out.println(jsonElement.getClass()); } }