반응형
JSON 오브젝트목록 루프
웹 서비스에서 목록 <>을(를) JSON 개체 목록으로 반환합니다.for 루프를 사용하여 목록을 반복하고 속성에서 값을 가져오려고 합니다.반환되는 JSON의 예를 다음에 나타냅니다.
{"d":[{"__type":"FluentWeb.DTO.EmployeeOrder",
"EmployeeName":"Janet Leverling",
"EmployeeTitle":"Sales Representative",
"RequiredDate":"\/Date(839224800000)\/",
"OrderedProducts":null}]}
그래서 다음과 같은 방법으로 내용을 추출하려고 합니다.
function PrintResults(result) {
for (var i = 0; i < result.length; i++) {
alert(result.employeename);
}
어떻게 하면 좋을까요?
조심하세요.d리스트입니다.
for (var i = 0; i < result.d.length; i++) {
alert(result.d[i].employeename);
}
오늘도 같은 문제가 있었습니다.당신의 토픽이 도움이 되었습니다.해결책을 제시하겠습니다.
alert(result.d[0].EmployeeTitle);
거의 다 됐어!이것을 시험해 보세요.
for (var prop in result) {
if (result.hasOwnProperty(prop)) {
alert(result[prop]);
}
}
업데이트:
결과가 실제로 하나의 오브젝트 배열인 경우 다음과 같이 해야 합니다.
for (var prop in result[0]) {
if (result[0].hasOwnProperty(prop)) {
alert(result[0][prop]);
}
}
또는 어레이 내의 각 결과를 루프하고 싶은 경우는, 다음의 순서에 따릅니다.
for (var i = 0; i < results.length; i++) {
for (var prop in result[i]) {
if (result[i].hasOwnProperty(prop)) {
alert(result[i][prop]);
}
}
}
여기 있습니다.
success:
function(data) {
$.each(data, function(i, item){
alert("Mine is " + i + "|" + item.title + "|" + item.key);
});
}
샘플 JSON 텍스트:
{"title": "camp crowhouse",
"key": "agtnZW90YWdkZXYyMXIKCxIEUG9zdBgUDA"}
jQuery를 사용하고 있기 때문에 각 메서드를 사용하는 것이 좋습니다.또한, 이 JS 오브젝트 [주]에서는 모든 것이 속성 'd'의 값인 것 같습니다.
$.each(result.d,function(i) {
// In case there are several values in the array 'd'
$.each(this,function(j) {
// Apparently doesn't work...
alert(this.EmployeeName);
// What about this?
alert(result.d[i][j]['EmployeeName']);
// Or this?
alert(result.d[i][j].EmployeeName);
});
});
효과가 있을 겁니다. 그렇지 않다면 JSON의 더 긴 예를 제시해 주실 수 있을 겁니다.
편집: 이 중 어느 것도 작동하지 않으면 JSON 구문에 문제가 있는 것 같습니다.
var d = $.parseJSON(result.d);
for(var i =0;i<d.length;i++){
alert(d[i].EmployeeName);
}
이거면 될 거야!
$(document).ready(function ()
{
$.ajax(
{
type: 'POST',
url: "/Home/MethodName",
success: function (data) {
//data is the string that the method returns in a json format, but in string
var jsonData = JSON.parse(data); //This converts the string to json
for (var i = 0; i < jsonData.length; i++) //The json object has lenght
{
var object = jsonData[i]; //You are in the current object
$('#olListId').append('<li class="someclass>' + object.Atributte + '</li>'); //now you access the property.
}
/* JSON EXAMPLE
[{ "Atributte": "value" },
{ "Atributte": "value" },
{ "Atributte": "value" }]
*/
}
});
});
여기서 가장 중요한 것은 JSON 키-값 쌍의 속성과 동일한 속성을 사용하는 것입니다.
다음과 같은 전화가 걸려 왔습니다.
$('#select_box_id').change(function() {
var action = $('#my_form').attr('action');
$.get(action,{},function(response){
$.each(response.result,function(i) {
alert("key is: " + i + ", val is: " + response.result[i]);
});
}, 'json');
});
서버에서 돌아오는 구조는 다음과 같습니다.
{"result":{"1":"waterskiing","2":"canoeing","18":"windsurfing"}}
언급URL : https://stackoverflow.com/questions/800593/loop-through-json-object-list
반응형
'programing' 카테고리의 다른 글
| ChunkLoadError: 청크 노드_modules_next_dist_client_dev_noop_js를 로드하지 못했습니다. (0) | 2023.03.20 |
|---|---|
| react-native - 전체 화면 크기가 아닌 View를 포함하는 이미지에 맞춥니다. (0) | 2023.03.20 |
| 반응 DOMException:'노드'에서 'removeChild'를 실행하지 못했습니다.제거할 노드가 이 노드의 하위 노드가 아닙니다. (0) | 2023.03.20 |
| MongoDB 콘솔 클리어 방법 (0) | 2023.03.20 |
| CORS 비행 전 요청이 접근통제 검사를 통과하지 못했습니다.HTTP OK 상태가 아닙니다. (0) | 2023.03.20 |