1.Jackson
Remark : Json 필드 없으면 무시
오류:
1 2 3 |
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field |
Class 단위로 무시 넣기 .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
클래스 속성이 잘못되어도 그냥 무시함.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
public static void main(String[] args) throws IOException, LifecycleException { //SpringApplication.run(DemoApplication.class, args); DemoApplication demo = new DemoApplication(); JSONObject sJson = demo.TestJsonRead("Testdata.json"); //class ObjectMapper objectMapper = new ObjectMapper() .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false); try{ Sdata sdata = objectMapper.readValue(sJson.toJSONString(),Sdata .class ); System.out.println(sdata .getFarLst().size()); } catch (JsonParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JsonMappingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("ok"); } |
ObjectMapper 에 무시 넣기 @JsonIgnoreProperties(ignoreUnknown = true)
1 2 3 4 5 6 7 8 9 |
@JsonProperty("gkTrgtYn") @JsonIgnoreProperties(ignoreUnknown = true) public String getGkTrgtYn() { return this.gkTrgtYn; } public void setGkTrgtYn(String gkTrgtYn) { this.gkTrgtYn = gkTrgtYn; } String gkTrgtYn; |
2.Gson
Remark : Json 필드가 없으면 자동 무시 하나. 클래스 속성이 잘못되면 에러
1 2 3 |
Caused by: com.google.gson.JsonSyntaxException: java.lang.NumberFormatException: Expected an int but was 2.7 at line 1 column 1478620 path $.Lst[165].cardPromLst[0].Rate |
모델 클래스 속성 int
1 2 3 4 5 6 7 8 9 |
@JsonProperty("Rate") public int getRate() { return this.totRate; } public void setRate(int Rate) { this.Rate = Rate; } int Rate; |
수정 String
1 2 3 4 5 6 7 8 9 |
@JsonProperty("Rate") public String getRate() { return this.totRate; } public void setRate(String Rate) { this.Rate = Rate; } String Rate; |
Class 단위로 무시는 자동처리, 없으면 null 으로 대체 옵션: serializeNulls()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
package com.example.demo; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonElement; import com.google.gson.JsonParser; import org.apache.catalina.LifecycleException; import org.json.simple.JSONObject; import org.springframework.boot.autoconfigure.SpringBootApplication; import java.io.*; import java.net.HttpURLConnection; import java.net.URL; import service.SimpleRead; import vo.ScheAirFarRs.ScheAirFarRsVo; @SpringBootApplication public class DemoApplication { public static void main(String[] args) throws IOException, LifecycleException { //SpringApplication.run(DemoApplication.class, args); DemoApplication demo = new DemoApplication(); JSONObject sJson = demo.TestJsonRead("Testjson.json"); // 함수 GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.serializeNulls(); //없음면 null로 대체 Gson gson = gsonBuilder.create(); try { Sdata sdata =gson.fromJson(sJson.toJSONString(), Sdata .class); System.out.println(sdata.getFarLst().size()); } catch (Exception e) { throw new RuntimeException("Unexpected error during parsing body param.", e); } System.out.println("ok"); } public JSONObject TestJsonRead(String FileName) { SimpleRead simpleRead = new SimpleRead(); JSONObject sResult; sResult = simpleRead.ReadJson(FileName); // System.out.println(sResult.toJSONString()); return sResult; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
package service; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; @Controller public class SimpleRead { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @GetMapping("ReadJson") public JSONObject ReadJson(String Filename) { //ReadJson String RetVal = ""; JSONObject jsonObject = new JSONObject(); try { JSONParser parser = new JSONParser(); try(BufferedReader br = new BufferedReader(new FileReader(Filename))) { String line; while ((line = br.readLine()) != null) { RetVal = RetVal + line; //System.out.println(line); } } jsonObject = (JSONObject) parser.parse(RetVal); } catch (ParseException | IOException e) { logger.debug(e.toString()); throw new RuntimeException(e); } return jsonObject; } } |