duanrong3308 2016-05-01 18:21
浏览 58
已采纳

如何使用jQuery合并两个json对象?

I have two json objects data and foo, i want to merge them and create a single json object. The resultant json should have all the values from foo and the values from data. so far i have something like this

javascript:

  $('#btn').click(function(){
            var imdbid=$('#tst').val();
            var url = "http://www.omdbapi.com/?i="+imdbid+"&plot=full&r=json"
            $.ajax({
            url:url,
            dataType:'json',
            success:function (json) {
                 data=json
                 var foo=$('form').serializeJSON() // store json string 
                 var marged=$.extend(data,foo);
                 console.log(marged);
                }
            })   
            }) 

HTML:

<form id="myform">
<label>imdb id:</label><input type="text"  id="tst" name="tst"/></form><br/>
<label>comment:-</label><input type="text" id="comment" name="comment" /><br/>
<label>link:-</label><input type="text" name="link" id="link" /><br/>
<input type="button" value="search"  id="btn"/>
</form>

the first object contains data returned from api and foo contains the values of form.i want to marge both object on a success function and send to php. when i am trying to print out the data it's as follows

{Title: "Mother's Day", Year: "2016", Rated: "PG-13", Released: "29 Apr 2016", Runtime: "118 min"…}
Actors
:
"Britt Robertson, Jennifer Aniston, Julia Roberts, Timothy Olyphant"
Awards
:

imdbID
:
"tt4824302"
tst
:
"tt4824302"

it's getting first input value not showing the second two.

  • 写回答

2条回答 默认 最新

  • doudu7626 2016-05-02 03:48
    关注

    You're closing the FORM on this line:

    <label>imdb id:</label><input type="text"  id="tst" name="tst"/></form><br/>
    

    ... so it only gets the first input field ; )

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 MATLAB动图的问题
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名