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


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

Updated on: 04-Jul-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements