如何在java中创建JSON模板

bvn4nwqk  于 2023-05-12  发布在  Java
关注(0)|答案(6)|浏览(226)

我的用例是我有一个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对象中删除一些键是困难的。任何建议都可以赞赏。

disho6za

disho6za1#

使用JSON Path库,如JayWay。有了这个,转换你的exapmle的模板将是:

客户端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"
    } ]
}
ut6juiuv

ut6juiuv2#

你可以使用Jackson来序列化和反序列化json。我建议你创建单独的类来代表你的客户端数据。
我将向您展示一个示例,说明如何反序列化数据并将其Map到客户端JSON。
您可以从http://www.jsonschema2pojo.org/获得一些帮助,将jsonMap到pojo,从而为客户端2生成相应的类。
代码如下:

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;
    }
}
ecbunoof

ecbunoof3#

把它转换成XML然后创建一些XSLT怎么样?它的可读性和清晰度可能会更高。

gt0wga4j

gt0wga4j4#

我在java中使用libs(lombok和Jackson)将完整的工作示例推到GIT中,模板jsonMap到对象,对象Map到不同客户端的json。

gupuwyp2

gupuwyp25#

您的工作是将多级JSON数据输出为多种JSON格式。JSONPath可以做到这一点,但这个过程很麻烦。
一个简单的替代方法是使用SPL。SPL是一个Java开源包。你只需要三行代码就可以完成这项工作:
enter image description here
SPL提供JDBC驱动程序供Java调用。只需将上面的SPL脚本存储为jsonparse.splx,并在Java应用程序中调用它,就像调用存储过程一样:

…
Class.forName("com.esproc.jdbc.InternalDriver");
con= DriverManager.getConnection("jdbc:esproc:local://");
st = con.prepareCall("call jsonparse()");
st.execute();
…
avwztpqn

avwztpqn6#

Java并没有内置很好的模板。
但是,如果你想做一个快速的脏JSON模板,你可以替换一些值-特别是没有所有丑陋的引号转义:
{“key”:“value”}
您可以使用单引号和字符串替换:
'key':'VALUE '}.replace(“'”,“"”).replace(“VALUE”,42)
一些注意事项:
如果任何现有的键或值有单引号(如O 'malley),这将中断。
它不会将字符串替换为数字、布尔值或null
它本身不能插入嵌套的数组或对象(例如[] {}),而不是字符串。
但只要做一些额外的工作,你就可以完成80/20规则。在这之后,您可能希望研究解析或生成--但到那时,您并不需要快速模板。

相关问题