如何在java编程中模板化json

问题描述 投票:2回答:4

我的用例是我有一个json文件,但我只需要与客户端共享少数几个。例如:考虑源json文件,如下所示。

{
    "name": "XYZ",
    "age": 24,
    "education": {
        "college": "ppppp",
        "study": "b.tech",
        "grade": 6.8
    },
    "friends": ["kkkk",
    "bbbbbbbbbbb",
    "jjjjjj"],
    "dob":"01-08-1990"
}

对于客户端1,我必须分享以下输出

{
    "personalInfo": {
        "name": "XYZ",
        "age": 24,
        "friendsNames": ["kkkk","bbbbbbbbbbb","jjjjjj"]
    },
    "educationalInfo": {
        "college": "ppppp",
        "study": "b.tech",
        "grade": 6.8
    }
}

对于客户端2,我必须分享以下输出

{
    "personalInformation": {
        "nameOfEmployee": "XYZ",
        "ageOfEmployee": 24
    },
    "educationalInformation": {
        "college": "ppppp",
        "study": "b.tech"
    }
}

对于其他客户端,用例也是相同的,我必须跳过一些键并为键提供不同的名称。如何通过某种配置动态执行此操作。我使用jsonPath来实现这一点,但是从json对象中删除几个键是很困难的。任何建议都可以表示赞赏。

java json templates configuration
4个回答
1
投票

那么将它转换为XML然后创建一些XSLT呢?它可能更具可读性和清洁性。


1
投票

您可以使用Jackson来序列化和反序列化json。我建议你创建代表你的客户数据的单独的类。

我向您展示了如何反序列化数据并将其映射到客户端json的示例。

您可以从http://www.jsonschema2pojo.org/获取一些帮助来为客户端2生成相应的类,以便将json映射到pojo。

这是代码:

public class Main {
    public static void main(String[] args) throws ParseException, ParserConfigurationException, IOException, SAXException {

        ObjectMapper mapper = new ObjectMapper();

        Root root = mapper.readValue(new File("test.json"), Root.class);

        Client1 c1 = new Client1();
        PersonalInfo personalInfo1 = new PersonalInfo();
        personalInfo1.setAge(root.getAge());
        personalInfo1.setFriendsNames(root.getFriends());
        personalInfo1.setName(root.getName());

        EducationalInfo educationalInfo1 = new EducationalInfo();
        educationalInfo1.setCollege(root.getEducation().getCollege());
        educationalInfo1.setGrade(root.getEducation().getGrade());
        educationalInfo1.setStudy(root.getEducation().getStudy());

        c1.setPersonalInfo(personalInfo1);
        c1.setEducationalInfo(educationalInfo1);

        mapper.writeValue(new File("client1.json"), c1);
    }
}

里面的test.json文件:

{
  "name": "XYZ",
  "age": 24,
  "education": {
    "college": "ppppp",
    "study": "b.tech",
    "grade": 6.8
  },
  "friends": [
    "kkkk",
    "bbbbbbbbbbb",
    "jjjjjj"
  ],
  "dob": "01-08-1990"
}

在client1.json文件中:

{
  "personalInfo": {
    "name": "XYZ",
    "age": 24,
    "friendsNames": [
      "kkkk",
      "bbbbbbbbbbb",
      "jjjjjj"
    ]
  },
  "educationalInfo": {
    "college": "ppppp",
    "study": "b.tech",
    "grade": 6.8
  }
}

以下是表示json数据的类:

class Education {
    private String college;
    private String study;
    private float grade;

    public String getCollege() {
        return college;
    }

    public void setCollege(String college) {
        this.college = college;
    }

    public String getStudy() {
        return study;
    }

    public void setStudy(String study) {
        this.study = study;
    }

    public float getGrade() {
        return grade;
    }

    public void setGrade(float grade) {
        this.grade = grade;
    }
}

// root of your base json data
class Root {

    private String name;
    private int age;
    private Education education;
    private List<String> friends;
    private String dob;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Education getEducation() {
        return education;
    }

    public void setEducation(Education education) {
        this.education = education;
    }

    public List<String> getFriends() {
        return friends;
    }

    public void setFriends(List<String> friends) {
        this.friends = friends;
    }

    public String getDob() {
        return dob;
    }

    public void setDob(String dob) {
        this.dob = dob;
    }
}

class EducationalInfo {

    private String college;
    private String study;
    private float grade;

    public String getCollege() {
        return college;
    }

    public void setCollege(String college) {
        this.college = college;
    }

    public String getStudy() {
        return study;
    }

    public void setStudy(String study) {
        this.study = study;
    }

    public float getGrade() {
        return grade;
    }

    public void setGrade(float grade) {
        this.grade = grade;
    }
}

// class which represents client 1 json data
class Client1 {

    private PersonalInfo personalInfo;
    private EducationalInfo educationalInfo;

    public PersonalInfo getPersonalInfo() {
        return personalInfo;
    }

    public void setPersonalInfo(PersonalInfo personalInfo) {
        this.personalInfo = personalInfo;
    }

    public EducationalInfo getEducationalInfo() {
        return educationalInfo;
    }

    public void setEducationalInfo(EducationalInfo educationalInfo) {
        this.educationalInfo = educationalInfo;
    }
}

class PersonalInfo {

    private String name;
    private int age;
    private List<String> friendsNames = null;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public List<String> getFriendsNames() {
        return friendsNames;
    }

    public void setFriendsNames(List<String> friendsNames) {
        this.friendsNames = friendsNames;
    }
}

1
投票

使用JSON Path库,如JayWay。有了它,转换你的例子的模板将是:

客户1

{
    "personalInfo": {
        "name": "$.name",
        "age": "$.age",
        "friendsNames": "$.friends"
    },
    "educationalInfo": "$.education"
}

客户2

{
    "personalInformation": {
        "nameOfEmployee": "$.name",
        "ageOfEmployee": "$.age"
    },
    "educationalInformation": {
        "college": "$.education.college",
        "study": "$.education.study"
    }
}

您需要实现的是模板遍历。它大约有20行代码;如果您还需要转换列表元素,则为40,例如:

{
    "friends": [ "$.friends[?(@ =~ /.{5,}/)]", {
        "name": "@",
        "since": "$.dob"
    } ]
}

这将导致:

{
    "friends": [ {
        "name": "bbbbbbbbbbb",
        "since": "01-08-1990"
    }, {
        "name": "jjjjjj",
        "since": "01-08-1990"
    } ]
}

0
投票

我将java中的完整工作示例推送到带有libs的GIT(lombok和jackson)模板json映射到对象和对象到不同客户端的json。

© www.soinside.com 2019 - 2024. All rights reserved.