2014-04-07 4 views
0

두 개의 JSON 객체를 집계하고 난 견인 JSON 개체가 첫 번째 개체 에 결과를 할당 할 수있는 방법이와 내가에연결하여 두 JSON 객체

   <script type="text/javascript"> 
       $(document).ready(function() { 

        var FirstObject= [ 
        { "label": "salem", "actor": "" }, 
       { "label": "Aragorn", "actor": "Viggo Mortensen" }, 
       { "label": "Arwen", "actor": "Liv Tyler" }, 
       { "label": "Bilbo Baggins", "actor": "Ian Holm" }, 
       { "label": "Boromir", "actor": "Sean Bean" }, 
       { "label": "Frodo Baggins", "actor": "Elijah Wood" }, 
      { "label": "Gandalf", "actor": "Ian McKellen" }, 
       { "label": "Gimli", "actor": "John Rhys-Davies" }, 
       { "label": "Gollum", "actor": "Andy Serkis" }, 
      { "label": "Legolas", "actor": "Orlando Bloom" }, 
       { "label": "Meriadoc Merry Brandybuck", "actor": "Dominic Monaghan" }, 
       { "label": "Peregrin Pippin Took", "actor": "Billy Boyd" }, 
     { "label": "Samwise Gamgee", "actor": "Sean Astin" } 
       ]; 
     $("#search").keyup(function() { 


      $.ajax({ 
       type: "POST", 
       dataType: "json", 
       contentType: "application/json", 
       url: "WebService1.asmx/GetDocumentNames", 
       data: '{ }', 
       success: function (data) { 
        **FirstObject =data.d** 




       }, 
       error: function() { aler("Salem Error"); } 
      } 

    ); 
     }); 



    }); 


</script> 

그래서 하나의 객체로 그들을 통합 할 문 FirstObject는 = data.d 난 집계

+1

'$ .extend()'어쩌면? – Scimonster

+0

깊은 객체를 효과적으로 병합하는 jQuery의'extend'를보십시오 : http://api.jquery.com/jquery.extend/ – David

+0

두 객체를 "연결"하는 것은 혼란스러운 선택이고 나는 또한 "병합"을 원한다는 것을 이해했습니다. "두 개체,하지만 그는 실제로 두 개의 배열을 연결하거나, 배열에 개체를 추가하려는 것 같아요 ... – Djizeus

답변

3

: jQuery를 문서에서

// first argument tells jQuery's extend to deep copy the properties 
$.extend(true, FirstObject, data.d); 

인용 :

병합 두 개체 반복적으로, 첫 번째 수정.

0

예를 아래에서 살펴 보자 싶어 :

var jsonArray1 = [{'name': "doug", 'id':5}, {'name': "dofug", 'id':23}]; 
var jsonArray2 = [{'name': "goud", 'id':1}, {'name': "doaaug", 'id':52}]; 
jsonArray1 = jsonArray1.concat(jsonArray2); 
1

변수 FirstObject은 실제로 배열이고, 은 배열이 아니라 배열에 추가하거나 집계하는 JSON 객체이라고 가정하면 Array의 메소드 push을 호출하기 만하면됩니다. 당신이 받고있는 것은 JSON 배열이 아니라 그 JSON 개체 인 경우

FirstObject.push(data.d); 

, 당신은 배열의 concat 방법을 사용할 수 있습니다. 이 같은 사용 jQuery extend

FirstObject.concat(data.d); 
0

JSON 개체를 병합하고 공통 키를 유지하려는 경우.

var object1 = { 
 
    'key1': 10, 
 
    'key2': 'Hello ', 
 
    'key3': [ 
 
    'I', 
 
    'am' 
 
    ], 
 
    'key4': { 
 
    'key5': 'Hi', 
 
    'key6': 11 
 
    } 
 
}; 
 
var object2 = { 
 
    'key1': 11, 
 
    'key2': 'World', 
 
    'key3': [ 
 
    'an', 
 
    'Array' 
 
    ], 
 
    'key4': { 
 
    'key5': ' there', 
 
    'key6': '#SomeRandomString' 
 
    } 
 
}; 
 

 
function isArray(value) { 
 
    return Object.prototype.toString.call(value) === '[object Array]'; 
 
} 
 

 
function isObject(value) { 
 
    return Object.prototype.toString.call(value) === '[object Object]'; 
 
} 
 

 
var result = {}; 
 
for (var key in object1) { 
 
    if (object1.hasOwnProperty(key) && object2.hasOwnProperty(key)) { 
 
    
 
    //check if the value is of type array (works for key 3) 
 
    if (isArray(object1[key]) && isArray(object2[key])) { 
 
     result[key] = []; 
 
     for (var i in object1[key]) { 
 
     result[key].push(object1[key][i]) 
 
     } 
 
     for (var i in object2[key]) { 
 
     result[key].push(object2[key][i]) 
 
     } 
 
    } 
 
    
 
    //check if the value is of type object (works for key 4) 
 
    else if (isObject(object1[key])) { 
 
     result[key] = {}; 
 
     for (var key_inner in object1[key]) { 
 
     if (object1[key].hasOwnProperty(key_inner) && object2[key].hasOwnProperty(key_inner)) { 
 
      result[key][key_inner] = object1[key][key_inner] + object2[key][key_inner]; 
 
     } 
 
     } 
 
    } else { 
 
     result[key] = object1[key] + object2[key]; 
 
    } 
 
    } 
 
} 
 
//console.log(JSON.stringify(result)); 
 
console.log(result);