Home > Java > javaTutorial > How to convert JSON string to Bean using JSON-lib API in Java?

How to convert JSON string to Bean using JSON-lib API in Java?

王林
Release: 2023-09-17 20:33:06
forward
887 people have browsed it

如何使用Java中的JSON-lib API将JSON字符串转换为Bean?

JSON-lib API is a Java library for serializing and deserializing java beans, maps, arrays and collections in JSON format. We need to convert the JSON string to a bean by first converting the string to a JSON object and then converting it to a java bean.

Syntax

public static Object toBean(JSONObject jsonObject, Class beanClass)
Copy after login

In the following program we can convert JSON string to bean.

Example

import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
public class ConvertJSONStringToBeanTest {
   public static void main(String[] args) {
      String jsonStr = "{\"firstName\": \"Adithya\", \"lastName\": \"Sai\", \"age\": 30, \"technology\": \"Java\"}";
      JSONObject jsonObj = (JSONObject)JSONSerializer.toJSON(jsonStr); // convert String to JSON
      System.out.println(jsonObj);
     
      Student student = (Student)JSONObject.toBean(jsonObj, Student.class); // convert JSON to Bean
      System.out.println(student.toString());
   }
   public static class Student {
      private String firstName;
      private String lastName;
      private int age;
      private String technology;
      public Student() {
      }
      public String getFirstName() {
         return firstName;
      }
      public void setFirstName(String firstName) {
         this.firstName = firstName;
      }
      public String getLastName() {
         return lastName;
      }
      public void setLastName(String lastName) {
         this.lastName = lastName;
      }
      public int getAge() {
         return age;
      }
      public void setAge(int age) {
         this.age = age;
      }
      public String getTechnology () {
         return technology;
      }
      public void setTechnology(String technology) {
         this.technology = technology;
     }
      public String toString() {
         return "Student[ " +
         "firstName = " + firstName +
         ", lastName = " + lastName +
         ", age = " + age +
         ", technology = " + technology +
         " ]";
      }
   }
}
Copy after login

Output

{"firstName":"Adithya","lastName":"Sai","age":30,"technology":"Java"}
Student[ firstName = Adithya, lastName = Sai, age = 30, technology = Java ]
Copy after login

The above is the detailed content of How to convert JSON string to Bean using JSON-lib API in Java?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template