Java Parse JSON String To Object

Chapter: Miscellaneous Last Updated: 24-03-2023 18:10:18 UTC


            /* ............... START ............... */
Json sample to parse.
   "name": "John",
   "age": 30,
   "city": "New York"


import org.json.JSONObject;

String jsonString = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}";
JSONObject json = new JSONObject(jsonString);

String name = json.getString("name");
int age = json.getInt("age");
String city = json.getString("city");

// create a Person object
Person person = new Person(name, age, city);

                /* ............... END ............... */


  • The program takes a JSON string as input and parses it into a Java object.
  • To do this, the program uses the built-in JSON parsing library, org.json, which provides the JSONObject class for working with JSON objects.
  • First, the JSON string is stored in a variable called jsonString. This string represents a JSON object that contains three key-value pairs: "name", "age", and "city".
  • Next, the JSONObject class is used to parse the JSON string into a JSON object. This object contains the same three key-value pairs as the original JSON string.
  • To extract the values of the keys from the JSON object, the getString and getInt methods are used. These methods take the key as a parameter and return the corresponding value as a String or an int, respectively.
  • Finally, the program creates a Person object using the extracted values of the "name", "age", and "city" keys. This Person object is a custom class that would need to be defined separately in the program.
  • Overall, this program demonstrates how to use the org.json library to parse a JSON string into a Java object and extract the values of its keys.


#Java Parse JSON String To Object #JSON string to Java object online

Similar Programs Chapter Last Updated
Swap Two Numbers Without Using Third Variable In Java Miscellaneous 02-06-2023
Java Program To Find The Average Of An Array Of Numbers Miscellaneous 02-06-2023
How Do You Find The Factorial Of A Number In Java Miscellaneous 02-06-2023
Java Program That Takes Two Numbers As Input And Prints Their Sum Miscellaneous 27-05-2023
How To Get The Length Of An Array In Java Miscellaneous 27-05-2023
Java Add Element To List Example Miscellaneous 19-05-2023
Java Program To Square All Items In List Miscellaneous 17-05-2023
Java Program To Merge Two Lists Miscellaneous 17-05-2023
How To Reverse A List In Java Miscellaneous 17-05-2023
Java Program To Find Unique Elements In An Array Miscellaneous 14-05-2023
Java Program To List All Elements In List Miscellaneous 30-04-2023
Java Program To Create XML File Miscellaneous 23-04-2023
Java Program To Run Something Every X Minutes Miscellaneous 22-04-2023
Java Program To Reverse A String Miscellaneous 22-04-2023
Java Program To Implement Method Overloading Miscellaneous 16-04-2023
Java Program For Date And Time Miscellaneous 24-03-2023
Java Binary Search Tree Implementation Miscellaneous 21-03-2023
Java Program To Merge Two PDF Files Miscellaneous 18-03-2023
How To Create A Git Repository | Git repository commands Miscellaneous 31-07-2021
Currency Formatter In Java | How To Format Currency In Java Miscellaneous 19-07-2021
Factory Design Pattern In Java Miscellaneous 11-05-2021
Data Types In Java Miscellaneous 09-06-2018