JSON Parsing JSON string Parse JSON string using com.google.gson library in Java


com.google.gson library needs to be added to use this code.

Here is the example string:

String companyDetails = {"companyName":"abcd","address":"abcdefg"}

JSON strings can be parsed using below syntax in Java:

JsonParser parser =  new JsonParser();
JsonElement jsonElement = parser.parse(companyDetails);
JsonObject jsonObj = jsonElement.getAsJsonObject();
String comapnyName = jsonObj.get("companyName").getAsString();