Merge two JSON objects programmatically

后端 未结 8 1007
被撕碎了的回忆
被撕碎了的回忆 2020-12-17 02:01

I have two JSON objects here, generated through the Google Search API. The URL\'s of these objects can be found below.

http://ajax.googleapis.com/ajax/services/searc

8条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-17 02:18

    To make it more neat, you can add the merge function to the JSON object. This is the solution from Johan van de Merwe based on Elliot's answer, added to the actual JSON object.

    // Extend JSON object
    JSON.merge = function (o,ob) {
    
      for (var z in ob) {
        o[z] = ob[z];
      }
    
      return o;
    }
    
    json3 = JSON.merge(json1,json2);
    

提交回复
热议问题