JSON (JavaScript Object Notation) processing in Java involves parsing JSON data received from a web service or generating JSON data to send to a web service. Java provides several libraries for working with JSON, including `org.json`, Jackson, and Gson. Here's how you can parse and generate JSON data using these libraries:
Parsing JSON with `org.json`
import org.json.JSONObject;
public class JsonParsingExample {
public static void main(String[] args) {
// Sample JSON data
String jsonString = "{\"name\": \"John\", \"age\": 30, \"city\": \"New York\"}";
// Parse JSON string to JSONObject
JSONObject jsonObject = new JSONObject(jsonString);
// Access values
String name = jsonObject.getString("name");
int age = jsonObject.getInt("age");
String city = jsonObject.getString("city");
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("City: " + city);
}
}
Parsing JSON with Jackson
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.4</version>
</dependency>
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JacksonParsingExample {
public static void main(String[] args) throws Exception {
// Sample JSON data
String jsonString = "{\"name\": \"John\", \"age\": 30, \"city\": \"New York\"}";
// Parse JSON string to JsonNode
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(jsonString);
// Access values
String name = jsonNode.get("name").asText();
int age = jsonNode.get("age").asInt();
String city = jsonNode.get("city").asText();
System.out.println("Name: " + name);
System.out.println("City: " + city);
}
}
Parsing JSON with Gson
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.8</version>
</dependency>
import com.google.gson.JsonParser;
public class GsonParsingExample {
public static void main(String[] args) {
// Sample JSON data
String jsonString = "{\"name\": \"John\", \"age\": 30, \"city\": \"New York\"}";
// Parse JSON string to JsonObject
JsonObject jsonObject = JsonParser.parseString(jsonString).getAsJsonObject();
// Access values
String name = jsonObject.get("name").getAsString();
int age = jsonObject.get("age").getAsInt();
String city = jsonObject.get("city").getAsString();
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("City: " + city);
}
}
Generating JSON with `org.json`
public class JsonGenerationExample {
public static void main(String[] args) {
// Create JSONObject
JSONObject jsonObject = new JSONObject();
jsonObject.put("name", "John");
jsonObject.put("age", 30);
jsonObject.put("city", "New York");
// Convert JSONObject to JSON string
String jsonString = jsonObject.toString();
System.out.println(jsonString);
}
}
Generating JSON with Jackson
public class JacksonGenerationExample {
public static void main(String[] args) throws Exception {
// Create ObjectMapper
ObjectMapper mapper = new ObjectMapper();
// Create JSON object
Object jsonObject = mapper.createObjectNode()
.put("name", "John")
.put("age", 30)
.put("city", "New York");
// Convert JSON object to JSON string
String jsonString = mapper.writeValueAsString(jsonObject);
System.out.println(jsonString);
}
}
Generating JSON with Gson
public class GsonGenerationExample {
public static void main(String[] args) {
// Create JsonObject
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("name", "John");
jsonObject.addProperty("age", 30);
jsonObject.addProperty("city", "New York");
// Convert JsonObject to JSON string
String jsonString = jsonObject.toString();
System.out.println(jsonString);
}
}
Conclusion
JSON processing in Java involves parsing JSON data received from a web service or generating JSON data to send to a web service. Java provides several libraries for working with JSON, including `org.json`, Jackson, and Gson. By using these libraries, you can easily parse and generate JSON data in your Java applications.
Nenhum comentário:
Postar um comentário