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

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

当我们运行mvn generate-sources时,我们希望它生成这样的东西:

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行才能实现这一点?


当前回答

我也有同样的问题,所以我决定开始写一个小工具来帮助我解决这个问题。我要分享并开源它。

https://github.com/BrunoAlexandreMendesMartins/CleverModels

它支持,JAVA, c#和Objective-c从JSON。

欢迎投稿!

其他回答

你也可以试试GSON库。它非常强大,它可以从集合,自定义对象和作品中创建JSON,反之亦然。它是在Apache许可证2.0下发布的,所以你也可以在商业上使用它。

http://code.google.com/p/google-gson/

用最近的项目回答这个老问题;-)。

目前最好的解决方案可能是JsonSchema2Pojo:

它从很少使用的Json Schema中完成这项工作,但也可以使用纯Json。它提供了Ant和Maven插件,一个在线测试应用程序可以让你对这个工具有一个概念。我放了一个Json Tweet,并生成了所有包含类(Tweet,用户,位置等)。

我们将在Agorava项目中使用它来生成社交媒体映射,并遵循API的不断进化。

我知道有很多答案,但在所有这些答案中,我发现这一个对我最有用。下面的链接将所有POJO类放在一个单独的文件中,而不是像一些提到的网站那样放在一个巨大的类中:

https://json2csharp.com/json-to-pojo

它还有其他转换器。此外,它可以在线工作,不受大小限制。我的JSON是巨大的,它工作得很好。

这里有一个在线工具,它将接受JSON,包括嵌套对象或嵌套对象数组,并生成带有Jackson注释的Java源代码。

试试http://www.jsonschema2pojo.org

或者Maven的jsonschema2pojo插件:

<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模式,请删除这一行。

2014年更新:自2009年12月提出这个问题以来,发生了两件事:

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 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.