📌  相关文章
📜  使用 GSON 将 JSON 字符串转换为Java对象

📅  最后修改于: 2022-05-13 01:54:52.863000             🧑  作者: Mango

使用 GSON 将 JSON 字符串转换为Java对象

先决条件:使用 GSON 将Java对象转换为 Json 字符串
JSON 代表JavaScript 对象表示法。它是一种基于文本的标准格式,它显示基于 JavaScript 对象语法的结构化数据。它通常用于在 Web 应用程序中传输数据。强烈建议使用 JSON 在服务器和 Web 应用程序之间传输数据。
要将Java对象转换为 JSON,可以使用以下方法:

  • GSON:它是一个开源Java库,用于将Java对象序列化和反序列化为 JSON。
  • 杰克逊 API

在本文中,使用 GSON 将预定义的 JSON 字符串转换为Java对象。
例子:

执行此操作的步骤如下:

  • 添加 Jackson 的 jar 文件(如果是 Maven 项目,在 pom.xml 文件中添加 Gson 依赖项)
html

       com.google.code.gson
       gson
       2.6.2
   


Java
package GeeksforGeeks.Geeks;
public class Organisation {
    private String organisation_name;
    private String description;
    private int Employees;
 
    // Calling getters and setters
    public String getOrganisation_name()
    {
        return organisation_name;
    }
    public void setOrganisation_name(String organisation_name)
    {
        this.organisation_name = organisation_name;
    }
    public String getDescription()
    {
        return description;
    }
    public void setDescription(String description)
    {
        this.description = description;
    }
    public int getEmployees()
    {
        return Employees;
    }
    public void setEmployees(int employees)
    {
        Employees = employees;
    }
 
    // Creating toString
    @Override
    public String toString()
    {
        return "Organisation [organisation_name="
            + organisation_name
            + ", description="
            + description
            + ", Employees="
            + Employees + "]";
    }
}


Java
package GeeksforGeeks.Geeks;
 
import com.google.gson.Gson;
 
public class JsonToObject {
 
    public static void main(String[] args)
    {
        // Creating object of Organisation
        Organisation org = new Organisation();
 
        // Converting json to object
        org = getOrganisationObject();
 
        // Displaying object
        System.out.println(org);
    }
 
    private static Organisation getOrganisationObject()
    {
        // Storing preprocessed json(Added slashes)
        String OrganisationJson
            = "{\"organisation_name\"
            : \"GeeksforGeeks\",
            \"description\"
            : \"A computer Science portal for Geeks\",
            \"Employee\"
            : \"2000\"}";
 
        // Creating a Gson Object
        Gson gson = new Gson();
 
        // Converting json to object
        // first parameter should be preprocessed json
        // and second should be mapping class
        Organisation organisation
            = gson.fromJson(OrganisationJson,
                            Organisation.class);
 
        // return object
        return organisation;
    }
}


以下是显示此步骤的屏幕截图:-

  • 创建要转换为 JSON 的 POJO(Plain Old Java Object)

Java

package GeeksforGeeks.Geeks;
public class Organisation {
    private String organisation_name;
    private String description;
    private int Employees;
 
    // Calling getters and setters
    public String getOrganisation_name()
    {
        return organisation_name;
    }
    public void setOrganisation_name(String organisation_name)
    {
        this.organisation_name = organisation_name;
    }
    public String getDescription()
    {
        return description;
    }
    public void setDescription(String description)
    {
        this.description = description;
    }
    public int getEmployees()
    {
        return Employees;
    }
    public void setEmployees(int employees)
    {
        Employees = employees;
    }
 
    // Creating toString
    @Override
    public String toString()
    {
        return "Organisation [organisation_name="
            + organisation_name
            + ", description="
            + description
            + ", Employees="
            + Employees + "]";
    }
}

以下是显示此步骤的屏幕截图:-

  • 创建用于存储 Json 字符串的字符串变量:
    注意:这个 Json字符串不应该是一个简单的 Json 字符串。在将 JSON 字符串传递给 GSON 对象之前,对其进行预处理并添加斜杠。
    预处理示例:
  • 创建一个用于将 Json 转换为组织对象的Java类:

Java

package GeeksforGeeks.Geeks;
 
import com.google.gson.Gson;
 
public class JsonToObject {
 
    public static void main(String[] args)
    {
        // Creating object of Organisation
        Organisation org = new Organisation();
 
        // Converting json to object
        org = getOrganisationObject();
 
        // Displaying object
        System.out.println(org);
    }
 
    private static Organisation getOrganisationObject()
    {
        // Storing preprocessed json(Added slashes)
        String OrganisationJson
            = "{\"organisation_name\"
            : \"GeeksforGeeks\",
            \"description\"
            : \"A computer Science portal for Geeks\",
            \"Employee\"
            : \"2000\"}";
 
        // Creating a Gson Object
        Gson gson = new Gson();
 
        // Converting json to object
        // first parameter should be preprocessed json
        // and second should be mapping class
        Organisation organisation
            = gson.fromJson(OrganisationJson,
                            Organisation.class);
 
        // return object
        return organisation;
    }
}

以下是显示此步骤的屏幕截图:-

  • 执行流程

输出: