Home > front end >  Java convert String to JsonArray
Java convert String to JsonArray

Time:10-23

I am currently getting this response from a server:

"statusHistory": "[{\"status\":\"TEST_STATUS_2\",\"timestamp\":\"2022-10-22-11.16.22.519\"}]"

Which is produced by converting a JSONArray object to a string using the Jackson ObjectMapper. How can I retrieve this string and convert it back to a JSONArray?

I have tried using the objectMapper as objectMapper.readValue('string', JSONArray.class) and was unable to do that. I also attempted to map it directly as a JSONArray by instantiating a new JSONArray as JSONArray statusHistory = new JSONArray('string'); For reference, this is the unescaped JSON: [{"status":"TEST_STATUS_2","timestamp":"2022-10-22-11.16.22.519"}]

CodePudding user response:

If you have a JSON-object with the property "statusHistory" you can parse this object into a JsonNode using ObjectMapper.readTree().

And then access the JSON-array mapped to the property "statusHistory" using get() method.

To convert a JsonNode into a String simply invoke toString() on it:

String json = """
    {"statusHistory": "[{\\"status\\":\\"TEST_STATUS_2\\",\\"timestamp\\":\\"2022-10-22-11.16.22.519\\"}]\"}
""";
    
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(json);
String jsonArray = node.get("statusHistory").toString();
    
System.out.println(jsonArray);

Output:

"[{\"status\":\"TEST_STATUS_2\",\"timestamp\":\"2022-10-22-11.16.22.519\"}]"

CodePudding user response:

Probably below code snippet will be helpful.

import java.util.List;

import com.fasterxml.jackson.databind.ObjectMapper;

public class JSONArrayTest {

    public static void main(String[] args) {
        String data = "{\"statusHistory\": [{\"status\":\"TEST_STATUS_2\",\"timestamp\":\"2022-10-22-11.16.22.519\"}]}";
        ObjectMapper mapper = new ObjectMapper();
        try {
            StatusHistory statusHistory = mapper.readValue(data.getBytes(), StatusHistory.class);
            System.out.println(statusHistory);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

class StatusDetails {
    private String status;
    private String timestamp;
    public String getStatus() {
        return status;
    }
    public void setStatus(String status) {
        this.status = status;
    }
    public String getTimestamp() {
        return timestamp;
    }
    public void setTimestamp(String timestamp) {
        this.timestamp = timestamp;
    }
    @Override
    public String toString() {
        return String.format("StatusDetails [status=%s, timestamp=%s]", status, timestamp);
    }
    
}

class StatusHistory {
    private List<StatusDetails> statusHistory;

    public List<StatusDetails> getStatusHistory() {
        return statusHistory;
    }

    public void setStatusHistory(List<StatusDetails> statusHistory) {
        this.statusHistory = statusHistory;
    }

    @Override
    public String toString() {
        return String.format("StatusHistory [statusHistory=%s]", statusHistory);
    }
    
}
  • Related