jsonschema2pojo没有从json字符串生成pojo类

时间:2015-03-31 23:04:50

标签: java json

我正在关注链接Generate Java class from JSON?以从json字符串创建POJO类(而不是来自模式)。我使用版本0.4.10的jsonschema2pojo jar但无法生成POJO类。 我的代码如下,

public class App 
{
    public static void main( String[] args )
    {
        JCodeModel codeModel = new JCodeModel();        
        try {
            URL source = new URL("file:///C://Users//...//accession.json");
            new SchemaMapper().generate(codeModel, "Accession", "com.test", source);
            File dir = new File("D://test");
            if(dir.exists()){
                System.out.println("dir available");
                codeModel.build(dir);
            }else{
                System.out.println("dir not available");
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }       
    }
}

所以accession.json有json字符串需要转换为POJO。有人可以帮我。

4 个答案:

答案 0 :(得分:7)

我在使用命令行工具方面有类似的经历。就我而言,这是因为没有正确指定源类型(JSONSCHEMA或JSON;默认值:JSONSCHEMA)。

我认为你的问题很相似:你正在使用SchemaMapper的默认(no-args)构造函数。以下步骤应解决问题:

  1. 子类org.jsonschema2pojo.DefaultGenerationConfig,覆盖getSourceType()以返回SourceType.JSON
  2. SchemaMapper(RuleFactory ruleFactory, SchemaGenerator schemaGenerator)构造函数使用该子类的实例(而不是no-args构造函数)。

答案 1 :(得分:6)

一旦遇到同样的问题,我就解决了这个问题。 在您的代码中,您使用的是默认配置,它采用源类型Jason Schema。 但是当你输入Jason时,你必须以这种方式设置这个返回类型: 配置中的SourceType.JSON。

class App
{   
    public static void main( String[] args )
        {
            JCodeModel codeModel = new JCodeModel();        
            try {
                URL source= new URL("file:///D:/temp.json");
                GenerationConfig config = new DefaultGenerationConfig() {
                    @Override
                    public boolean isGenerateBuilders() {
                    return true;
                    }
                    public SourceType getSourceType(){
                        return SourceType.JSON;
                    }
                    };
                    SchemaMapper mapper =new SchemaMapper(new RuleFactory(config, new GsonAnnotator(), new SchemaStore()), new SchemaGenerator());
                    mapper.generate(codeModel, "Accession", "com.test", source);
                File dir = new File("D://");
                if(dir.exists()){
                    System.out.println("dir available");
                    codeModel.build(dir);
                }else{
                    System.out.println("dir not available");
                }

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }       
        }
}

我希望它会帮助你.. 祝你好运!!

答案 2 :(得分:1)

我也有点挣扎。我最后做了以下事情:

创建我自己的GenerationConfig,覆盖 getSourceType

static class MyConfig extends DefaultGenerationConfig {
    @Override
    public SourceType getSourceType() {
        return SourceType.JSON;
    }
}

然后我按如下方式初始化了构建过程:

private void parseFileExample() {               
    URL source = new URL("file:/tmp/input/blah.json");
    JCodeModel codeModel = new JCodeModel();
    MyConfig generationConfig = new MyConfig();
    RuleFactory ruleFactory = new RuleFactory(generationConfig, new GsonAnnotator(), new SchemaStore());
    SchemaGenerator generator = new SchemaGenerator();

    SchemaMapper mapper = new SchemaMapper(ruleFactory, generator);
    mapper.generate(codeModel, "MyClass", "com.drakedroid", source);

    codeModel.build(new File("/tmp/output/"));
}

这里的诀窍是,使用网址。当我传入一个字符串时, mapper.generate 不起作用。

答案 3 :(得分:0)

谢谢@Kapil,你的回答帮助了我。 该程序允许我们根据预定义的JSON生成POJO类。 我们也可以在运行时使用它,其中JSON响应未知,将JSON响应写入文件并使用以下代码相应地读取它。

public class JSONExample {

  public static void main(String... args)  {

    JCodeModel codeModel = new JCodeModel();

    try
    {
        // In sample.json I have already pasted a JSON
        File file=new File("//root//AndroidStudioProjects//MyApplication//sample.json");
        URL source = file.toURI().toURL();

        GenerationConfig config = new DefaultGenerationConfig() {
            @Override
            public boolean isGenerateBuilders() 
            {
                return true;
            }
            public SourceType getSourceType()
            {
                return SourceType.JSON;
            }
        };

        SchemaMapper mapper = new SchemaMapper(new RuleFactory(config, new Jackson2Annotator(), new SchemaStore()), new SchemaGenerator());
        try {
            // The ClassName is the main class that will contain references to other generated class files
            // com.example is the package name 
            mapper.generate(codeModel, "ClassName", "com.example", source);
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            // Directory where classes will be genetrated
            File file1=new File("//root//AndroidStudioProjects//MyApplication//");
            if (file1.exists())
            {
                System.out.println("dir available");
                codeModel.build(file1);
            }
            else
            {
                System.out.println("dir not available");
            }

            System.out.println(""+file1+" Exists "+file1.exists());


        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    catch(Exception e)
    {
        e.printStackTrace();
    }
 }
}