首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将JSON文件转换为Java对象,但java对象提供的值为空值

将JSON文件转换为Java对象,但java对象提供的值为空值
EN

Stack Overflow用户
提问于 2018-08-07 23:34:12
回答 1查看 180关注 0票数 0

我有一个JSON文件,其中的内容如下:

代码语言:javascript
复制
 {
  "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。输出如下所示。

代码语言:javascript
复制
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来判断。

下面是从在线实用程序生成的concepts类:

代码语言:javascript
复制
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+"]";
    }
}

下面是我尝试施展魔法的主要方法:

代码语言:javascript
复制
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();
        }
    }
}

也许我没有正确使用这个库,但我认为我使用的方法是正确的。如果有人能提供一些的话。洞察力非常感谢:)

EN

回答 1

Stack Overflow用户

发布于 2018-08-08 06:55:09

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51730548

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档