本文介绍了使用杰克逊解析Json文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

{
   "TestSuite":{
      "TestSuiteInfo":{
         "-description":"parse"
      },
      "TestCase":[
         {
            "TestCaseData":{
               "-sequence":"sequential",
               "-testNumber":"2",
               "-testCaseFile":"testcase\\Web\\Ab.xml"
            }
         },
         {
            "TestCaseData":{
               "-sequence":"sequential",
               "-testNumber":"3",
               "-testCaseFile":"testcase\\Web\\BC.xml"
            }
         }
      ]
   }
}

我的Pojos是:

public class TestSuite {

    private TestSuiteInfo testSuiteInfo;
    private TestCase listOfTestCases;

    public TestSuiteInfo getTestSuiteInfo() {
        return testSuiteInfo;
    }

    public void setTestSuiteInfo(TestSuiteInfo testSuiteInfo) {
        this.testSuiteInfo = testSuiteInfo;
    }

    public TestCase getListOfTestCases() {
        return listOfTestCases;
    }

    public void setListOfTestCases(TestCase listOfTestCases) {
        this.listOfTestCases = listOfTestCases;
    }
}


public class TestSuiteInfo {

    private String description;

    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
}


import java.util.Iterator;
import java.util.List;

public class TestCase {

    private List<TestCaseData> testCaseData;

    public List<TestCaseData> getTestCaseData() {
        return testCaseData;
    }

    public void setTestCaseData(List<TestCaseData> testCaseData) {
        this.testCaseData = testCaseData;
    }
}


public class TestCaseData {

    private String sequence;
    private int testNumber;
    private String testCaseFile;

    public String getSequence() {
        return sequence;
    }

    public void setSequence(String sequence) {
        this.sequence = sequence;
    }

    public int getTestNumber() {
        return testNumber;
    }

    public void setTestNumber(int testNumber) {
        this.testNumber = testNumber;
    }

    public String getTestCaseFile() {
        return testCaseFile;
    }

    public void setTestCaseFile(String testCaseFile) {
        this.testCaseFile = testCaseFile;
    }
}

我之前没有使用过杰克逊,真的很感激如果有人可以帮助我解析文件和获取对象。
我试图从过去的两天解析这个,但没有取得任何成功

I haven't use Jackson before, will really appreciate if anyone could help me in parsing the file and getting the objects.I am trying to parse this from past two days, but didnt got any success

推荐答案

通常要解析JSON杰克逊图书馆,你会使用 ObjectMapper 这样的类:

Usually to parse JSON with the Jackson library, you would use the ObjectMapper class like this:

public static void main(final String[] args) {
    final String json = "some JSON string";
    final ObjectMapper mapper = new ObjectMapper();
    final TestSuite readValue = mapper.readValue(json, TestSuite.class);
    //Then some code that uses the readValue.
    //Keep in mind that the mapper.readValue() method does throw some exceptions
    //So you'll need to handle those too.
}

但是,我写了一个快速测试类来检查你的JSON的解析并且遇到了一些问题。

However, I wrote a quick test class to check out the parsing of your JSON and came across some issues.

基本上,JSON的设计和域的设计不匹配。因此,您可以更改JSON,也可以更改域对象。

Basically, the design of the JSON and the design of the domain don't match up. So you can either alter the JSON, or you can alter the domain objects.

更改JSON以适应域


  1. 其中包含 - 的属性名称不会在jackson中很好地解析,因此需要将其删除。

  2. 在每个对象之前拥有类名不会有帮助。杰克逊希望这些属性属性,因此类名称将需要删除或替换属性名称。

  3. 必须提供属性名称,因为它们位于域对象中,以便jackson解析它们。你不能只说这是一个对象,然后开始一个列表,列表必须有一个属性名称/

我之后在JSON中调整了这些东西,我用它来解析提供的域对象。我最终得到的JSON看起来像这样:

After I'd adjusted a these things in the JSON, I got it to parse with the provided domain objects. The JSON I ended up with looked like this:

{
   "testSuiteInfo":{
      "description":"parse"
   },
   "listOfTestCases":{
      "testCaseData":[
         {
            "sequence":"sequential",
            "testNumber":"2",
            "testCaseFile":"testcase\\Web\\Ab.xml"
         },
         {
            "sequence":"sequential",
            "testNumber":"3",
            "testCaseFile":"testcase\\Web\\BC.xml"
         }
      ]
   }
}

这是我的测试方法,它解析上面的篡改JSON(请忽略所有转义字符)

Here's my test method that does parse the doctored JSON above (please ignore all the escape characters)

public static void main(final String[] args) {
    final String json = "{\"testSuiteInfo\":{\"description\":\"parse\"}," +
            "\"listOfTestCases\":{" +
            "\"testCaseData\":[" +
            "{\"sequence\":\"sequential\",\"testNumber\":\"2\",\"testCaseFile\":\"testcase\\\\Web\\\\Ab.xml\"}," +
            "{\"sequence\":\"sequential\",\"testNumber\":\"3\",\"testCaseFile\":\"testcase\\\\Web\\\\BC.xml\"}" +
            "]" +
            "}" +
            "}";

    final ObjectMapper mapper = new ObjectMapper();

    try {
        final TestSuite readValue = mapper.readValue(json, TestSuite.class);
        System.out.println(readValue.getListOfTestCases()); //just a test to see if the object is built
    }
    catch (final Exception e) {
        e.printStackTrace();
    }
}

更改域以适合JSON

首先,主要问题是将类名称作为属性标识符。这使得以通常的方式使用此JSON非常困难。我必须添加几个包装类来绕过JSON中的类名。

Firstly, the main issues is having the Class names as the property identifiers. That makes it quite difficult to work with this JSON in the usual manner. I've had to add a couple of wrapper classes to get around the class names being in the JSON.


  • 我已经添加了一个 OverallWrapper 类,它具有 TestSuite 属性,以满足JSON中的TestSuite类名称。

  • I've added an OverallWrapper class that has a TestSuite property to cater for the TestSuite class name in the JSON.

我还添加了一个 TestCaseDataWrapper 类来满足列表中的TestCaseData类名称JSON。

I've also added a TestCaseDataWrapper class to cater for the TestCaseData class names in the list in the JSON.

我一起删除了TestCase类,因为它刚刚成为其他类之一的属性。

I removed the TestCase class all together as that just became a property on one of the other classes.

然后为了使属性名称与对象匹配,我使用了 @JsonProperty 注释。

Then to make the property names match up with the objects, I've used the @JsonProperty annotation.

以下是修改后的类,以及工作和解析JSON的终极解析器测试方法。 (再次,请原谅JSON字符串中的所有转义字符)

Here are the classes after the modifications, and the ultimate parser test method that works and parses the JSON. (again, excuse all the escape characters in the JSON string)

import org.codehaus.jackson.annotate.JsonProperty;

public class OverallWrapper {

    private TestSuite testSuite;

    @JsonProperty("TestSuite")
    public TestSuite getTestSuite() {
        return this.testSuite;
    }

    public void setTestSuite(final TestSuite testSuite) {
        this.testSuite = testSuite;
    }
}



import java.util.List;
import org.codehaus.jackson.annotate.JsonProperty;

public class TestSuite {

    private TestSuiteInfo testSuiteInfo;

    private List<TestCaseDataWrapper> testCaseData;

    @JsonProperty("TestCase")
    public List<TestCaseDataWrapper> getTestCaseData() {
        return this.testCaseData;
    }

    public void setTestCaseData(final List<TestCaseDataWrapper> testCaseData) {
        this.testCaseData = testCaseData;
    }

    @JsonProperty("TestSuiteInfo")
    public TestSuiteInfo getTestSuiteInfo() {
        return this.testSuiteInfo;
    }

    public void setTestSuiteInfo(final TestSuiteInfo testSuiteInfo) {
        this.testSuiteInfo = testSuiteInfo;
    }
}



import org.codehaus.jackson.annotate.JsonProperty;

public class TestSuiteInfo {

    private String description;

    @JsonProperty("-description")
    public String getDescription() {
        return this.description;
    }
    public void setDescription(final String description) {
        this.description = description;
    }
}



import org.codehaus.jackson.annotate.JsonProperty;

public class TestCaseDataWrapper {

    @JsonProperty("TestCaseData")
    private TestCaseData testcaseData;

    public TestCaseData getTestcaseData() {
        return this.testcaseData;
    }

    public void setTestcaseData(final TestCaseData testcaseData) {
        this.testcaseData = testcaseData;
    }
}



import org.codehaus.jackson.annotate.JsonProperty;

public class TestCaseData {

    private String sequence;
    private int testNumber;
    private String testCaseFile;

    @JsonProperty("-sequence")
    public String getSequence() {
        return this.sequence;
    }

    public void setSequence(final String sequence) {
        this.sequence = sequence;
    }

    @JsonProperty("-testNumber")
    public int getTestNumber() {
        return this.testNumber;
    }

    public void setTestNumber(final int testNumber) {
        this.testNumber = testNumber;
    }

    @JsonProperty("-testCaseFile")
    public String getTestCaseFile() {
        return this.testCaseFile;
    }

    public void setTestCaseFile(final String testCaseFile) {
        this.testCaseFile = testCaseFile;
    }
}



public static void main(final String[] args) {

    final String json = "{\"TestSuite\":{\"TestSuiteInfo\":{\"-description\":\"parse\"},\"TestCase\":[" +
            "{\"TestCaseData\":{\"-sequence\":\"sequential\",\"-testNumber\":\"2\",\"-testCaseFile\":\"testcase\\\\Web\\\\Ab.xml\"}}," +
            "{\"TestCaseData\":{\"-sequence\":\"sequential\",\"-testNumber\":\"3\",\"-testCaseFile\":\"testcase\\\\Web\\\\BC.xml\"}}" +
            "]}}";

    final ObjectMapper mapper = new ObjectMapper();

    try {
        final OverallWrapper readValue = mapper.readValue(json, OverallWrapper.class);

        System.out.println(readValue.getTestSuite());
    }
    catch (final Exception e) {
        e.printStackTrace();
    }
}

总结

最终的问题是该域名不与JSON结合。

The ultimate issue is that the domain doesn't marry up with the JSON.

我个人更喜欢更改JSON与域名结合,因为域名在设计中似乎有意义,并且需要较少的自定义和强制。

Personally I prefer to change the JSON to marry up to the domain, as the domain seems to make sense in it's design and requires less customization and forcing.

但是,我接受你可能没有这个选择,因此重新设计了域名。

However, I do accept that you may not have that choice, hence the redesign of the domain.

这篇关于使用杰克逊解析Json文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-21 06:02