简体   繁体   中英

Java - Extract array String from JSON and convert to JSON format

I have a JSON String as below:

"{ \"password\":\"des123\",\"ROU_DATA\":[{\"FORM_RECEIVING_TIME\":\"12:00:00\",\"REMARKS\":\"Redemption of Unit\"}, {\"FORM_RECEIVING_TIME\":\"13:00:00\",\"REMARKS\":\"sALE of Unit\"}] }";

Now I want to extract the Array from it and need to use it as a separate pojo class so that I can iterate over each value..

Now the problem is, when I try to convert the complete String to Map and get the Array value from the map.. It transforms its format to MAp format like:

{FORM_RECEIVING_DATE = 12:00:00, etc..}

However json string should be {"FORM_RECEIVING_DATE": "12:00:00", etc..}

due to the MAp format its now allowing me to parse it using my POJO Class..

Please help to convert it to my JSONFormat ...

**NOTE: Please note that I can only use Jackson **.

CLASS A

            ObjectMapper mapper2 = new ObjectMapper();

            Map<String, Object> map;

            map = mapper2.readValue(json, new TypeReference<Map<String, Object>>(){});

            System.out.println("map: " + map.get("ROU_DATA") );


            String array = map.get("ROU_DATA").toString();

            String json2 = new ObjectMapper().writeValueAsString(array.replace("[", "").replace("]", ""));
            String json3 = new ObjectMapper().writeValueAsString(json2);
            System.out.println("json2>>" + json2);
            System.out.println("json2>>" + json3);
            mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
            // 1. convert JSON array to Array objects


            ROU[] pp1 = mapper.readValue("{" + array.replace("=", ":") + "}", ROU[].class);


            for (ROU person : pp1) {
                System.out.println(person.getRemarks());
            }

CLASS B

import com.fasterxml.jackson.annotation.JsonProperty;

public class ROU {

    @JsonProperty("FORM_RECEIVING_TIME")
    private String formdate;

    @JsonProperty("REMARKS")
    private String remarks;

    public String getFormdate() {
        return formdate;
    }

    public void setFormdate(String formdate) {
        this.formdate = formdate;
    }

    public String getRemarks() {
        return remarks;
    }

    public void setRemarks(String remarks) {
        this.remarks = remarks;
    }



}

map.get("ROU_DATA") returns a List object, and the toString() method of List does not generate JSON text.

You don't need to convert back to a JSON text just to get the ROU[] created, just call convertValue(...) .

String input = "{ \"password\":\"des123\",\"ROU_DATA\":[{\"FORM_RECEIVING_TIME\":\"12:00:00\",\"REMARKS\":\"Redemption of Unit\"}, {\"FORM_RECEIVING_TIME\":\"13:00:00\",\"REMARKS\":\"sALE of Unit\"}] }";

ObjectMapper mapper2 = new ObjectMapper();
Map<?, ?> json = mapper2.readValue(input, Map.class);
ROU[] pp1 = mapper2.convertValue(json.get("ROU_DATA"), ROU[].class);

for (ROU person : pp1)
    System.out.println(person.getRemarks());

Output

Redemption of Unit
sALE of Unit

class A

public class ROU {

@JsonProperty("FORM_RECEIVING_TIME")
private String formdate;

@JsonProperty("REMARKS")
private String remarks;

public String getFormdate() {
    return formdate;
}

public void setFormdate(String formdate) {
    this.formdate = formdate;
}

public String getRemarks() {
    return remarks;
}

public void setRemarks(String remarks) {
    this.remarks = remarks;
}

}

class B

public class ObjOuter {

 private String password;

 @JsonProperty("ROU_DATA")
 private List<ROU> rous;

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public List<ROU> getRous() {
    return rous;
}

public void setRous(List<ROU> rous) {
    this.rous = rous;
}

}

json to Object

    ObjectMapper mapper = new ObjectMapper();
    try {
        ObjOuter outer = mapper.readValue(str, ObjOuter.class);
        for (ROU rou : outer.getRous()) {
            System.out.println(rou.getFormdate());
            System.out.println(rou.getRemarks());
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

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