Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

read a JSON object in java [duplicate]

Tags:

java

json

jsp

How can we read a JSON object in a JSP, Servlet or in any other Java program?

like image 264
Sadesh Kumar N Avatar asked Nov 25 '09 16:11

Sadesh Kumar N


People also ask

How to read the contents of a JSON file in Java?

To read the contents of a JSON file using a Java program −. Instantiate the JSONParser class of the json-simple library. JSONParser jsonParser = new JSONParser (); Parse the contents of the obtained object using the parse () method.

How to read individual values from an array of JSON objects?

JSONObject jsonObject = (JSONObject)object; Using the get () method of the JSONObject class get the individual values. String name = (String) jsonObject.get ("Name"); Long age = (Long) jsonObject.get ("Age"); For reading the array of values, we will be using JSONArray class and read the individual values.

How to retrieve jsonobjects from jsonarray?

Retrieve your JSONObjects from JSONArray using getJSONObject (index) function 6.) Fetch the data from JSONObject using index '"interestKey"'. Note : JSON parsing uses the escape sequence for special nested characters if the json response (usually from other JSON response APIs) contains quotes ( ") like this

What is json JSON?

JSON (JavaScript Object Notation) is a lightweight, text-based, language-independent data exchange format that is easy for humans and machines to read and write. JSON can represent two structured types: objects and arrays. An object is an unordered collection of zero or more name/value pairs.


1 Answers

I would recommend using Gson for this. It has the advantage that it supports generics very well and it is also performant. I've posted a Gson#fromJson() example before here: Converting JSON to Java

like image 190
BalusC Avatar answered Sep 22 '22 13:09

BalusC