jsonschema2pojo不从json字符串生成pojo类 [英] jsonschema2pojo not generating pojo classes from json string

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

问题描述

我正在跟踪从JSON生成Java类?来自json字符串(而不是来自架构)的类.我正在使用版本0.4.10的jsonschema2pojo jar,但无法生成POJO类. 我的代码如下,

I was following the link Generate Java class from JSON? to create the POJO classes from json string (and not from schema). I am using jsonschema2pojo jar of version 0.4.10 but could not able to generate the POJO class. My code is as below,

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.有人可以在这里帮助我吗?

So accession.json has json string which need to be converted into POJO. Can anybody please help me here.

推荐答案

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

I had a similar experience using the command-line tool. In my case, it was the result of not correctly specifying the source type (JSONSCHEMA or JSON; default: JSONSCHEMA).

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

I think your problem is similar: You're using the default (no-args) constructor for SchemaMapper. The following steps should solve the problem:

  1. 子类org.jsonschema2pojo.DefaultGenerationConfig,重写getSourceType()以返回SourceType.JSON
  2. 使用该子类的实例作为SchemaMapper(RuleFactory ruleFactory, SchemaGenerator schemaGenerator)构造函数(而不是no-args构造函数).
  1. Subclass org.jsonschema2pojo.DefaultGenerationConfig, overriding getSourceType() to return SourceType.JSON
  2. Use an instance of that subclass for the SchemaMapper(RuleFactory ruleFactory, SchemaGenerator schemaGenerator) constructor (instead of the no-args constructor).

这篇关于jsonschema2pojo不从json字符串生成pojo类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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