简体   繁体   中英

How read value from properties file and put into JSON file

I have liquibase.json file. Threa are:

"runningAs": {
            "username": "Programer200"
          }

I try to creat custom property:

liquibaseuser=root

Is it possible put value of liquibaseuser to liquibase.json ?

You could parse the json to a model and change the values.

import com.fasterxml.jackson.databind.ObjectMapper;

public class App
{
    public static void main(String[] args)
    {
        // 1 read property
        String property = "root";
        // 2 read json
        String json = "{\"username\": \"Programer200\"}";
        // 3 map json to model
        ObjectMapper mapper = new ObjectMapper();
        MyModel jsonObject = mapper.readValue(json, MyModel.class);
        // 4 change variables
        jsonObject.username = property;
        System.out.println(mapper.writer().writeValueAsString(jsonObject));
    }
}


class MyModel
{
    public String username;
}

You can utilize the below library for the data access.

<dependency>
    <groupId>com.jayway.jsonpath</groupId>
    <artifactId>json-path</artifactId>
    <version>2.4.0</version>
</dependency

After access the data you can modify the Json String. But firstly you require to get the data like below.

String user = JsonPath.read(json, "$.runningAs.username");

System.out.println(user);

In your quandary, it seem's like you require key and value, then you can utilize the map.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM