Created
May 31, 2018 03:58
-
-
Save circlee/561e0b08e9636ea883d6aa727cd82e23 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// JavaScript | |
function sortObject(object){ | |
if(!object) { | |
return object; | |
} | |
var sortedObj = {}, | |
keys = Object.keys(object); | |
keys.sort(function(key1, key2){ | |
key1 = key1.toLowerCase(), key2 = key2.toLowerCase(); | |
if(key1 < key2) return -1; | |
if(key1 > key2) return 1; | |
return 0; | |
}); | |
for(var index in keys){ | |
var key = keys[index]; | |
if(typeof object[key] == 'object' && !(object[key] instanceof Array)){ | |
sortedObj[key] = sortObject(object[key]); | |
} else if(typeof object[key] == 'object' && (object[key] instanceof Array)){ | |
var tempArr = [] | |
object[key].forEach(function(item){ tempArr.push(sortObject(item)) }); | |
sortedObj[key] = tempArr; | |
} else { | |
sortedObj[key] = object[key]; | |
} | |
} | |
return sortedObj; | |
} | |
function sortObject_backup(object){ | |
var sortedObj = {}, | |
keys = Object.keys(object); | |
keys.sort(function(key1, key2){ | |
key1 = key1.toLowerCase(), key2 = key2.toLowerCase(); | |
if(key1 < key2) return -1; | |
if(key1 > key2) return 1; | |
return 0; | |
}); | |
for(var index in keys){ | |
var key = keys[index]; | |
if(typeof object[key] == 'object' && !(object[key] instanceof Array)){ | |
sortedObj[key] = sortObject(object[key]); | |
} else { | |
sortedObj[key] = object[key]; | |
} | |
} | |
return sortedObj; | |
} | |
// response 문자열을 가져오자 | |
var response = SampleResult.getResponseDataAsString(); | |
// javascript object 로 파싱 | |
var jsonOutput = JSON.parse(response); | |
// object를 key 순서대로 정렬 | |
var sortOutput = sortObject(jsonOutput); | |
// console 에 로그 한번 찍어주자 | |
log.info(JSON.stringify(sortOutput)); | |
// 다시 json문자열로 만들어서 , Response 에 담아주자 | |
var sortStr =JSON.stringify(sortOutput); | |
SampleResult.setResponseData(sortStr, 'utf-8'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment