Remark : Json 형식을 XML 로 변경
1.Jackson ObjectMapper 사용
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.io.IOException; import java.io.StringWriter; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.dataformat.xml.XmlMapper; import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator; public class SimpleTesting{ public static void main(String[] args) throws IOException{ final String jsonStr = "{\"name\":\"JSON\",\"integer\":1,\"double\":2.0,\"boolean\":true,\"nested\":{\"id\":42},\"array\":[1,2,3]}"; ObjectMapper jsonMapper = new ObjectMapper() .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);; JsonNode node = jsonMapper.readValue(jsonStr, JsonNode.class); XmlMapper xmlMapper = new XmlMapper(); xmlMapper.configure(SerializationFeature.INDENT_OUTPUT, true); xmlMapper.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true); xmlMapper.configure(ToXmlGenerator.Feature.WRITE_XML_1_1, true); StringWriter sw = new StringWriter(); xmlMapper.writeValue(sw, node); System.out.println(sw.toString()); } } |
2.Gson 사용
Spring
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 |
package com.example.demo; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.dataformat.xml.XmlMapper; import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator; 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.JsonNode; @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("TestData.json"); //class 정의 GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.serializeNulls(); Gson gson = gsonBuilder.create(); try { JsonNode node =gson.fromJson(sJson.toJSONString(), JsonNode.class); XmlMapper xmlMapper = new XmlMapper(); xmlMapper.configure(SerializationFeature.INDENT_OUTPUT, true); xmlMapper.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true); xmlMapper.configure(ToXmlGenerator.Feature.WRITE_XML_1_1, true); StringWriter sw = new StringWriter(); xmlMapper.writeValue(sw, node); System.out.println(sw.toString()); } catch (Exception e) { throw new RuntimeException("Unexpected error during parsing body param.", e); } System.out.println("ok"); } |