본문 바로가기
반응형

JSON6

[Kotlin] Json String (List) 을 Data Class 로 변환 (GSON) Json String (List) 을 Data Class 로 변환 (GSON) 이전 글에서 휴일 정보를 Json 으로 가져왔습니다. 참고 https://date.nager.at/api/v2/publicholidays/2023/KR 위의 호출 결과를 Data Class 로 매핑할 경우 아래와 같은 data class 로 만들 수 있습니다. data class PublicHolidays( @SerializedName("date") val date: Date, @SerializedName("localName") val dateName: String, @SerializedName("name") val engName: String, @SerializedName("countryCode") val locale: St.. 2023. 8. 2.
[Javascript] JSON Object key, value 가져오기 JSON 의 모든 키와 값을 출력하는 방법 const json = { "name": "bryan", "age": 87, "city": "Suwon" }; console.log("방법 1"); for (var key in json) { console.log(key + " : " + json[key]); } console.log("방법 2"); Object.entries(json).forEach( ([key, value]) => console.log(`${key} : ${value}`) ); console.log("방법 3"); Object.keys(json).forEach(key => { console.log(`${key} : ${json[key]}`); }); 하지만, json 안에 또다른 json ob.. 2023. 1. 28.
[SpringBoot] json 문자열 받아서 model class 에 맵핑하기 여러 클라이언트에서 Spring Boot 로 요청을 보내려고 할 때, Json 만큼 편한건 없는것 같습니다. json 문자열을 보냈을 때 Spring 에서 model class 에 맵핑하는 방법을 정리해 보겠습니다. 1. Single json 아래와 같은 json 문자열이 있을 때 {"name": "bryan", "city": "Seoul", "age": 18, "cars": ["GV80", "X6", "GLE350"]} Java(SpringBoot)에서는 다음과 같이 model class 를 준비합니다. import lombok.Getter; import lombok.Setter; import lombok.ToString; import java.util.List; @Getter @Setter @ToSt.. 2021. 10. 21.
[SpringBoot] RestApi 만들기 (2) JSON 형식 리턴 SpringBoot REST API JSON 지난 포스트에 이어서 테스트 api 에서 json 으로 리턴하는 방법을 알아보겠습니다. 지난 글 : SpringBoot Project 생성 및 심플한 RestApi 작성 1. 원시적인 방법 (json 문자열 만들기) @RestController public class InfoController { @GetMapping("/info") public String projectInfo() { //return "Project name is preword."; return "{\"project name\": \"preword\"," + "\"created date\": \"2021-07-03\"}"; } } 이렇게 만들면 오타의 위험도 있고, 너무 복잡하고 타이핑도 많.. 2021. 7. 3.
[Python] json 파일에 주석 달기 json file 에 주석달기 config.json 이라는 파일의 내용이 아래와 같다고 합시다. { "name": "bryan",/* 여기에 설명 쓰고싶을 때 */ "title": "helllo", "url": "https://hello-bryan.tistory.com/", "bookmark": true /* 여기에도 주석 달고 싶을 때 */ } 이 파일을 json 로드한다면 오류가 나겠죠. with open('config.json') as f: json_data = json.load(f) # error 아래와 같이 사용하시면 됩니다. with open('config.json', 'r', encoding='utf-8') as f: contents = f.read() while "/*" in content.. 2021. 2. 11.
[javascript] Json Key 가져오기 #keys #getOwnPropertyNames JS 의 json key 가져오기 Object.keys() var jsn = { a : 'hello~', b : 'bryan' }; var keys = Object.keys(jsn); console.log(keys) ["a", "b"] Object.getOwnPropertyNames() var jsn = { a : 'hello~', b : 'bryan' }; var keys = Object.getOwnPropertyNames(jsn); console.log(keys) ["a", "b"] 비표준 브라우저 호환 ( explorer 9 이하 ) var jsn = { a : 'hello~', b : 'bryan' }; for( var k in jsn ) { console.log(k + ":" + jsn[k]);.. 2020. 12. 18.
728x90
반응형