Home > Software engineering >  Java uses snakeyaml read and write yaml
Java uses snakeyaml read and write yaml

Time:11-07

When I use snakeyaml to convert the yaml file into a class, the List object cannot be converted correctly.

The following can work

public class UserYaml {
    private Integer test1;
    private String test2;
    private Test3 test3;
//    private List<Test> test;
}
test1: 123
test2: "wqre"
test3:
  testt1: 1
  testt2: "asd"

#test4:
#  - test:
#      a: string
#      b: 3
#  - test:
#      a: integer
#      b: 4

CodePudding user response:

When you use space in yaml file means you have craeted children for that variable, so you must be create another class as Test3 and reference it to UserYaml class:

public class UserYaml {
    private Integer test1;
    private String test2;
    private List<Test3> test3;
}

public class Test3{
    private Integer testt1;
    private String testt2;
}

CodePudding user response:

Sorry, I didn't make it clear. I want Test4 to work correctly.

public class UserYaml {
private Integer test1;
private String test2;
private Test3 test3;
private List<Test> test;}
test1: 123
test2: "wqre"
test3:
  testt1: 1
  testt2: "asd"

test4:
  - test:
      a: string
      b: 3
  - test:
      a: integer
      b: 4
Exception in thread "main" Cannot create property=test4 for JavaBean=UserYaml(test1=123, test2=wqre, test3=Test3(testt1=1, testt2=asd), test=null)
 in 'reader', line 1, column 1:
    test1: 123
    ^
Unable to find property 'test4' on class: UserYaml
 in 'reader', line 8, column 3:
      - test:
  • Related