Friday, January 24, 2025
HomeProgrammingHow to Parse Json in Java

How to Parse Json in Java

In Java, you can parse JSON using libraries like Jackson, Gson, or org.json.

  1. Using Jackson:
java
import com.fasterxml.jackson.databind.ObjectMapper;
String jsonString = "{\"name\":\"John\", \"age\":30}";
ObjectMapper objectMapper = new ObjectMapper();
MyClass obj = objectMapper.readValue(jsonString, MyClass.class);
  1. Using Gson:
java
import com.google.gson.Gson;
String jsonString = "{\"name\":\"John\", \"age\":30}";
Gson gson = new Gson();
MyClass obj = gson.fromJson(jsonString, MyClass.class);
  1. Using org.json:
java
import org.json.JSONObject;
String jsonString = "{\"name\":\"John\", \"age\":30}";
JSONObject obj = new JSONObject(jsonString);

In these examples, MyClass should be a class with the appropriate fields (name, age) for mapping the JSON data.

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x