Contents
see List작성일 2016. 08. 18.
AngularJS JSON Parsing 및 $$hashKey 오류 해결
AngularJS에서 객체를 JSON으로 변환하여 Spring 서버로 Ajax 전송할 때 발생하는 $$hashKey 관련 오류를 해결하는 방법입니다.
문제 상황
AngularJS의 ng-repeat 등에서 사용되는 객체에는 $$hashKey라는 내부 속성이 자동으로 추가됩니다. 이 속성이 서버로 전송되면 Jackson 등에서 파싱 오류가 발생할 수 있습니다.
해결 방법
// angular.toJson() 사용 - $$hashKey 자동 제거
angular.toJson(object);
Ajax 요청 시 적용
$http({
method: "POST",
url: "/api/save",
data: angular.toJson(myObject),
headers: {
"Content-Type": "application/json"
}
}).then(function(response) {
console.log("성공");
});
JSON.stringify vs angular.toJson 차이
var obj = {name: "test", $$hashKey: "object:123"};
// JSON.stringify - $$hashKey 포함
JSON.stringify(obj);
// 결과: {"name":"test","$$hashKey":"object:123"}
// angular.toJson - $$hashKey 제외
angular.toJson(obj);
// 결과: {"name":"test"}
ng-repeat에서 track by 사용
<!-- $$hashKey 생성 방지 -->
<li ng-repeat="item in items track by item.id">
{{item.name}}
</li>
<!-- 인덱스로 추적 -->
<li ng-repeat="item in items track by $index">
{{item.name}}
</li>
Spring Controller에서 처리
// Jackson에서 알 수 없는 속성 무시
@JsonIgnoreProperties(ignoreUnknown = true)
public class MyDto {
private String name;
// getters, setters
}
// 또는 전역 설정
objectMapper.configure(
DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false
);
angular.copy() 사용
// 깊은 복사로 $$hashKey 제거
var cleanObj = angular.copy(originalObj);
// 또는 배열의 경우
var cleanArray = angular.copy(originalArray);
수동으로 $$hashKey 제거
function removeHashKey(obj) {
if (Array.isArray(obj)) {
return obj.map(removeHashKey);
}
if (obj !== null && typeof obj === "object") {
var result = {};
for (var key in obj) {
if (key !== "$$hashKey") {
result[key] = removeHashKey(obj[key]);
}
}
return result;
}
return obj;
}javascript
| No | 작성일 | Title |
|---|---|---|
| 2163 | 2026. 02. 11. | React Server Components 심층 이해와 활용 |
| 2162 | 2026. 02. 11. | Next.js 15 App Router 완전 정복 가이드 |
| 2053 | 2025. 11. 30. | CSS-in-JS vs Tailwind CSS 비교 |
| 2052 | 2025. 11. 30. | Jest와 React Testing Library 테스트 작성 |
| 2051 | 2025. 11. 30. | Zustand - 간단한 상태 관리 라이브러리 |
| 2050 | 2025. 11. 30. | React Query(TanStack Query)로 서버 상태 관리 |
| 2049 | 2025. 11. 30. | Webpack에서 Vite로 마이그레이션 |
| 2048 | 2025. 11. 30. | JavaScript ES2024 새 기능 |
| 2047 | 2025. 11. 30. | Vue 3 Composition API 활용법 |
| 2046 | 2025. 11. 30. | Next.js App Router 마이그레이션 가이드 |