2015-03-31 61 views
4

我正在关注链接Generate Java class from JSON?从json字符串(而不是从模式)创建POJO类。我正在使用版本0.4.10的jsonschema2pojo jar,但无法生成POJO类。 我的代码如下,jsonschema2pojo不从json字符串生成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字符串。任何人都可以在这里帮助我。

+1

你有什么问题?如果你有个例外就把它放在问题上。 – 2015-03-31 23:39:12

+0

代码工作正常,但它没有生成测试目录下的输出POJO类 – 2015-04-01 03:18:49

回答

6

我有类似的使用命令行工具的经验。在我的情况下,它是不正确指定源类型(JSONSCHEMA或JSON;默认值:JSONSCHEMA)的结果。

我认为你的问题是类似的:你使用SchemaMapper的默认(无参数)构造函数。下面的步骤应解决的问题:

  1. 子类org.jsonschema2pojo.DefaultGenerationConfig,覆盖getSourceType()返回SourceType.JSON
  2. 使用该子类的实例为SchemaMapper(RuleFactory ruleFactory, SchemaGenerator schemaGenerator)构造函数(而不是无参数的构造函数)。
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/")); 
} 

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

4

一旦我面临同样的问题,然后我解决了这个问题。 在您的代码中,您使用的是采用源类型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(); 
      }  
     } 
} 

我希望它会帮助你.. GOOD LUCK !!

+0

这实际上对我很有用。 – dbustosp 2016-07-12 18:09:08

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