从 JSON 生成 Java 类? [英] Generate Java class from JSON?

查看:33
本文介绍了从 JSON 生成 Java 类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java Maven项目中,如何从JSON生成java源文件?例如我们有

In a Java Maven project, how do you generate java source files from JSON? For example we have

{
  "firstName": "John",  
  "lastName": "Smith",  
  "address": {  
    "streetAddress": "21 2nd Street",  
     "city": "New York"
  }
}

当我们运行 mvn generate-sources 时,我们希望它生成如下内容:

When we run mvn generate-sources we want it to generate something like this:

class Address  {
    JSONObject mInternalJSONObject;
     
    Address (JSONObject json){
        mInternalJSONObject = json;
    }
     
    String  getStreetAddress () {
        return mInternalJSONObject.getString("streetAddress");
    }
    
    String  getCity (){
        return mInternalJSONObject.getString("city");
    }
}

class Person {        
    JSONObject mInternalJSONObject;
    
    Person (JSONObject json){
        mInternalJSONObject = json;
    }
    
    String  getFirstName () {
        return mInternalJSONObject.getString("firstName");
    }
    
    String  getLastName (){
        return mInternalJSONObject.getString("lastName");
    }
    
    Address getAddress (){
        return Address(mInternalJSONObject.getString("address"));
    }
}

作为 Java 开发人员,我需要在 pom.xml 中编写哪些 XML 行才能实现这一点?

As a Java developer, what lines of XML do I need to write in my pom.xml in order to make this happen?

推荐答案

试试 http://www.jsonschema2pojo.org

或者 jsonschema2pojo Maven 插件:

Or the jsonschema2pojo plug-in for Maven:

<plugin>
    <groupId>org.jsonschema2pojo</groupId>
    <artifactId>jsonschema2pojo-maven-plugin</artifactId>
    <version>1.0.2</version>
    <configuration>
        <sourceDirectory>${basedir}/src/main/resources/schemas</sourceDirectory>
        <targetPackage>com.myproject.jsonschemas</targetPackage>
        <sourceType>json</sourceType>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
        </execution>
    </executions>
</plugin>

<sourceType>json</sourceType> 涵盖了源是 json 的情况(如 OP).如果您有实际的 json 模式,请删除此行.

The <sourceType>json</sourceType> covers the case where the sources are json (like the OP). If you have actual json schemas, remove this line.

2014 年更新:自 09 年 12 月提出此问题以来发生了两件事:

Updated in 2014: Two things have happened since Dec '09 when this question was asked:

  • JSON Schema 规范 发生了很大变化.它仍处于草案中(未定稿),但已接近完成,现在是指定结构规则的可行工具

  • The JSON Schema spec has moved on a lot. It's still in draft (not finalised) but it's close to completion and is now a viable tool specifying your structural rules

我最近启动了一个专门用于解决您的问题的新开源项目:jsonschema2pojo.jsonschema2pojo 工具采用 json 模式文档并生成 DTO 样式的 Java 类(以 .java 源文件的形式).该项目尚未成熟,但已经涵盖了 json 模式中最有用的部分.我正在寻求用户的更多反馈,以帮助推动开发.现在,您可以从命令行或 Maven 插件中使用该工具.

I've recently started a new open source project specifically intended to solve your problem: jsonschema2pojo. The jsonschema2pojo tool takes a json schema document and generates DTO-style Java classes (in the form of .java source files). The project is not yet mature but already provides coverage of the most useful parts of json schema. I'm looking for more feedback from users to help drive the development. Right now you can use the tool from the command line or as a Maven plugin.

希望这有帮助!

这篇关于从 JSON 生成 Java 类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆