【问题标题】:jsonschema2pojo not generating pojo classes from json stringjsonschema2pojo 没有从 json 字符串生成 pojo 类
【发布时间】:2015-06-05 11:12:40
【问题描述】:

我正在关注链接 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 有需要转换成 POJO 的 json 字符串。有谁能帮帮我。

【问题讨论】:

  • 您遇到了什么问题?如果您有例外,请将其放在问题上。
  • 代码工作正常,但没有在测试目录下生成输出POJO类

标签: java json


【解决方案1】:

我在使用命令行工具时也有类似的经历。就我而言,这是未正确指定源类型(JSONSCHEMA 或 JSON;默认值:JSONSCHEMA)的结果。

我认为您的问题是相似的:您正在为SchemaMapper 使用默认(无参数)构造函数。以下步骤应该可以解决问题:

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

【讨论】:

    【解决方案2】:

    一旦我遇到同样的问题,然后我解决了这个问题。 在您的代码中,您使用的是采用源类型 Jason 模式的默认配置。 但是当您输入 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();
                }       
            }
    }
    

    希望对你有帮助。。 祝你好运!!

    【讨论】:

      【解决方案3】:

      我也为此感到有些挣扎。我最后做了以下事情:

      创建了我自己的 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/"));
      }
      

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

      【讨论】:

        【解决方案4】:

        感谢@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();
            }
         }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-01-13
          • 2015-03-07
          • 2016-08-31
          • 2014-01-15
          • 2023-02-17
          • 2021-04-24
          • 2019-06-23
          相关资源
          最近更新 更多