将JSON文件转换为Java对象,但是Java对象为我提供了空值

时间:2018-08-07 15:34:12

标签: java json gson

我有一个JSON文件,其内容如下所示:

 {
  "unstructured": [
    {
      "data": {
        "concepts": [
          {
            "cui": "C4878",
            "preferredName": "LUNG CARCINOMA",
            "source": "wfg_cancer_iml",
            "sourceVersion": "v1.0",
            "type": "CANCER",
            "begin": 12,
            "end": 23,
            "coveredText": "lung cancer",
            "negated": false,
            "hypothetical": false,
            "disambiguationData": {
              "validity": "NO_DECISION"
            },
            "lemma": "LUNG CARCINOMA"
          }
        ],
        "negatedSpans": [
          {
            "trigger": {
              "begin": 33,
              "end": 36,
              "coveredText": "not"
            },
            "type": "NegatedSpan",
            "begin": 37,
            "end": 42,
            "coveredText": "smoke"
          }
        ],
        "hypotheticalSpans": [
          {
            "type": "HypotheticalSpan",
            "begin": 44,
            "end": 101,
            "coveredText": "She may consider chemotherapy as part of a treatment plan",
            "trigger": [
              {
                "begin": 52,
                "end": 60,
                "coveredText": "consider",
                "source": "internal"
              }
            ]
          }
        ]
      }
    }
  ]
}

我使用了一个在线实用工具,从上面的JSON生成类。我正在尝试将json反序列化为java对象,但是代码返回null。输出看起来像这样。

ClassPojo [lemma = null, source = null, preferredName = null, sourceVersion = null, disambiguationData = null, hypothetical = null, coveredText = null, cui = null, type = null, negated = null, end = null, begin = null]

显然,这些值不为null。从上面的JSON可以看出。

这是从在线实用程序生成的概念类:

public class Concepts
{
    private String lemma;

    private String source;

    private String preferredName;

    private String sourceVersion;

    private DisambiguationData disambiguationData;

    private String hypothetical;

    private String coveredText;

    private String cui;

    private String type;

    private String negated;

    private String end;

    private String begin;

    public String getLemma ()
    {
        return lemma;
    }

    public void setLemma (String lemma)
    {
        this.lemma = lemma;
    }

    public String getSource ()
    {
        return source;
    }

    public void setSource (String source)
    {
        this.source = source;
    }

    public String getPreferredName ()
    {
        return preferredName;
    }

    public void setPreferredName (String preferredName)
    {
        this.preferredName = preferredName;
    }

    public String getSourceVersion ()
    {
        return sourceVersion;
    }

    public void setSourceVersion (String sourceVersion)
    {
        this.sourceVersion = sourceVersion;
    }

    public DisambiguationData getDisambiguationData ()
    {
        return disambiguationData;
    }

    public void setDisambiguationData (DisambiguationData disambiguationData)
    {
        this.disambiguationData = disambiguationData;
    }

    public String getHypothetical ()
    {
        return hypothetical;
    }

    public void setHypothetical (String hypothetical)
    {
        this.hypothetical = hypothetical;
    }

    public String getCoveredText ()
    {
        return coveredText;
    }

    public void setCoveredText (String coveredText)
    {
        this.coveredText = coveredText;
    }

    public String getCui ()
    {
        return cui;
    }

    public void setCui (String cui)
    {
        this.cui = cui;
    }

    public String getType ()
    {
        return type;
    }

    public void setType (String type)
    {
        this.type = type;
    }

    public String getNegated ()
    {
        return negated;
    }

    public void setNegated (String negated)
    {
        this.negated = negated;
    }

    public String getEnd ()
    {
        return end;
    }

    public void setEnd (String end)
    {
        this.end = end;
    }

    public String getBegin ()
    {
        return begin;
    }

    public void setBegin (String begin)
    {
        this.begin = begin;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [lemma = "+lemma+", source = "+source+", preferredName = "+preferredName+", sourceVersion = "+sourceVersion+", disambiguationData = "+disambiguationData+", hypothetical = "+hypothetical+", coveredText = "+coveredText+", cui = "+cui+", type = "+type+", negated = "+negated+", end = "+end+", begin = "+begin+"]";
    }
}

这是我尝试做魔术的主要方法:

public class JsonToJavaObject {

    public static void main(String[] args) {

        Gson gson = new Gson();
        try (Reader reader = new FileReader("/Users/edgarjohnson/eclipse-workspace/JsonToJavaObject/src/Test.json")) {

            // Convert JSON to Java Object
            Concepts metadata = gson.fromJson(reader, Concepts.class);

            System.out.println(metadata.toString());
        } 
        catch (IOException e) {
            e.printStackTrace();
        }
    }
}

也许我没有正确使用该库,但是我认为我使用了正确的方法。如果有人可以请提供一些。洞察力,将不胜感激:)

1 个答案:

答案 0 :(得分:0)

我敢打赌,您输入的JSON不是您认为的那样。您必须提供与预期对象匹配的JSON。