Convert a JSON String to Java Object using the json-simple library in Java?

JavaJSONObject Oriented ProgrammingProgramming

The JSON is one of the widely used data-interchange formats and is a lightweight and language independent. The json.simple is a lightweight JSON processing library that can be used to encode or decode a JSON text. 

In the below program, we can convert a JSON String to Java object using the json.simple library.

Example

import org.json.simple.*;
import org.json.simple.parser.*;
public class ConvertJSONStringToObjectTest {
   public static void main(String[] args) {
      String jsonString = "{\"Name\":\"Raja\",\"EmployeeId\":\"115\",\"Age\":\"30\"}";
      JSONParser parser = new JSONParser();
      JSONObject obj;
      try {
         obj = (JSONObject)parser.parse(jsonString);
         System.out.println(obj.get("Name"));
         System.out.println(obj.get("EmployeeId"));
         System.out.println(obj.get("Age"));
      } catch(ParseException e) {
         e.printStackTrace();
      }
   }
}

Output

Raja
115
30
raja
Published on 17-Sep-2019 18:38:26
Advertisements