JSON Parser | Community
Skip to main content
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Techaspect_Solu

Hi,

The GSON JsonParser class can parse a JSON string or stream into a tree structure of Java objects. Before you can use the GSON JsonParser you must create a JsonParser instance. Here is an example of creating a JsonParser instance:

JsonParser jsonParser = new JsonParser();

Parsing JSON Into a Tree Structure:

Once you have created a JsonParser you can parse JSON into a tree structure with it. Here is an example of parsing a JSON string into a tree structure of GSON objects with the JsonParser:

JsonParser parser = new JsonParser();

String json = "{ \"key1\":\"Hello\",\"key2\":{\"key3:\":\"World\"}}";

JsonElement jsonTree = parser.parse(json);

The parsing of the JSON happens in the third line of code, by calling parse() on the JsonParser, passing as parameter a reference to the JSON string (or stream) to parse.

The above JsonElement (jsonTree) can be converted into JsonObject as -

JsonObject jsonObj = jsonTree.getAsJsonObject(); //converting JsonElement  into JsonObject

jsonObj .get("key1"); //gets key1 from json

In this way, GSON API can be used for parsing the JSON and getting desired result out of it.

NOTE: GSON is the alternative of deprecated JSON API in AEM 6.3.

For more information on this, please refer:

Gson (Gson 2.6.2 API)

Gson 2.6.2 API

Hope this helps!

Regards,

TechAspect Solutions

4 replies

Techaspect_Solu
Techaspect_SoluAccepted solution
Level 7
May 31, 2018

Hi,

The GSON JsonParser class can parse a JSON string or stream into a tree structure of Java objects. Before you can use the GSON JsonParser you must create a JsonParser instance. Here is an example of creating a JsonParser instance:

JsonParser jsonParser = new JsonParser();

Parsing JSON Into a Tree Structure:

Once you have created a JsonParser you can parse JSON into a tree structure with it. Here is an example of parsing a JSON string into a tree structure of GSON objects with the JsonParser:

JsonParser parser = new JsonParser();

String json = "{ \"key1\":\"Hello\",\"key2\":{\"key3:\":\"World\"}}";

JsonElement jsonTree = parser.parse(json);

The parsing of the JSON happens in the third line of code, by calling parse() on the JsonParser, passing as parameter a reference to the JSON string (or stream) to parse.

The above JsonElement (jsonTree) can be converted into JsonObject as -

JsonObject jsonObj = jsonTree.getAsJsonObject(); //converting JsonElement  into JsonObject

jsonObj .get("key1"); //gets key1 from json

In this way, GSON API can be used for parsing the JSON and getting desired result out of it.

NOTE: GSON is the alternative of deprecated JSON API in AEM 6.3.

For more information on this, please refer:

Gson (Gson 2.6.2 API)

Gson 2.6.2 API

Hope this helps!

Regards,

TechAspect Solutions

smacdonald2008
Level 10
May 31, 2018

Great response!

smacdonald2008
Level 10
May 31, 2018
girishk14
girishk14Author
Level 2
May 31, 2018

Thanks all ,it was helpful