2016-12-22 3 views
1

나는 다음과 같은 JSON 반환하는 web API이 다음과 같이AJAX 호출 후 웹 API 응답에서 동일한 JSON 형식을 유지 관리하려면 어떻게해야합니까?

[{"MovieId":2,"Title":"End Game","Year":"2013","Actors":"Cuba Gooding Jr., Angie Harmon, James Woods, Patrick Fabian","Plot":"A secret Service agent and a news reporter investigate the conspiracy behind the assassination of the President","Director":"Andy Cheng","Cover":"Endgame-movie-cover.jpg"},{"MovieId":3,"Title":"Saving Private Ryan","Year":"1998","Actors":"Tom Hanks, Tom Sizemore, Edward Burns, Barry Pepper","Plot":"Following the Normandy Landings, a group of U.S. soldiers go behind enemy lines to retrieve a paratrooper whose brothers have been killed in action","Director":"Steven Spielberg","Cover":"Saving-Private-Ryan-Cover.jpg"}] 

나는 테이블에 데이터를 채울 수있는 jqGrid에 대한 응답에서 JSON을 전달하려면를 내 AJAX 호출 후에 보이는 :

function fetchMovies(){ 
    $.ajax({ 
     url: 'http://orionadmin.azurewebsites.net/api/movies', 
     type: 'GET', 
     datatype: 'JSON', 
     contentType: 'application/json', 
     success: function (data) { 
      grid_data1 = JSON.stringify(data); 
      alert(grid_data); 
     }, 
     error: function (x, h, r) { 
      alert('Something went wrong') 
     } 
    }); 
} 
,이있는 jqGrid 소요되는 형식은

"[{"MovieId":2,"Title":"End Game","Year":"2013","Actors":"Cuba Gooding Jr., Angie Harmon, James Woods, Patrick Fabian","Plot":"A secret Service agent and a news reporter investigate the conspiracy behind the assassination of the President","Director":"Andy Cheng","Cover":"Endgame-movie-cover.jpg"},{"MovieId":3,"Title":"Saving Private Ryan","Year":"1998","Actors":"Tom Hanks, Tom Sizemore, Edward Burns, Barry Pepper","Plot":"Following the Normandy Landings, a group of U.S. soldiers go behind enemy lines to retrieve a paratrooper whose brothers have been killed in action","Director":"Steven Spielberg","Cover":"Saving-Private-Ryan-Cover.jpg"}]" 

TABL에 데이터를 채우는 데 다음과 같이 AJAX 호출의

내 결과가 보인다 E :

<script type="text/javascript"> 
    var grid_data = 
    [ 
     {id:"1",name:"Desktop Computer",note:"note",stock:"Yes",ship:"FedEx", sdate:"2007-12-03"}, 
     {id:"2",name:"Laptop",note:"Long text ",stock:"Yes",ship:"InTime",sdate:"2007-12-03"}, 
     {id:"3",name:"LCD Monitor",note:"note3",stock:"Yes",ship:"TNT",sdate:"2007-12-03"}, 
     {id:"4",name:"Speakers",note:"note",stock:"No",ship:"ARAMEX",sdate:"2007-12-03"}, 
     {id:"5",name:"Laser Printer",note:"note2",stock:"Yes",ship:"FedEx",sdate:"2007-12-03"}, 
     {id:"6",name:"Play Station",note:"note3",stock:"No", ship:"FedEx",sdate:"2007-12-03"}, 
     {id:"7",name:"Mobile Telephone",note:"note",stock:"Yes",ship:"ARAMEX",sdate:"2007-12-03"}, 
     {id:"8",name:"Server",note:"note2",stock:"Yes",ship:"TNT",sdate:"2007-12-03"}, 
     {id:"9",name:"Matrix Printer",note:"note3",stock:"No", ship:"FedEx",sdate:"2007-12-03"}, 
     {id:"10",name:"Desktop Computer",note:"note",stock:"Yes",ship:"FedEx", sdate:"2007-12-03"}, 
     {id:"11",name:"Laptop",note:"Long text ",stock:"Yes",ship:"InTime",sdate:"2007-12-03"}, 
     {id:"12",name:"LCD Monitor",note:"note3",stock:"Yes",ship:"TNT",sdate:"2007-12-03"}, 
     {id:"13",name:"Speakers",note:"note",stock:"No",ship:"ARAMEX",sdate:"2007-12-03"}, 
     {id:"14",name:"Laser Printer",note:"note2",stock:"Yes",ship:"FedEx",sdate:"2007-12-03"}, 
     {id:"15",name:"Play Station",note:"note3",stock:"No", ship:"FedEx",sdate:"2007-12-03"}, 
     {id:"16",name:"Mobile Telephone",note:"note",stock:"Yes",ship:"ARAMEX",sdate:"2007-12-03"}, 
     {id:"17",name:"Server",note:"note2",stock:"Yes",ship:"TNT",sdate:"2007-12-03"}, 
     {id:"18",name:"Matrix Printer",note:"note3",stock:"No", ship:"FedEx",sdate:"2007-12-03"}, 
     {id:"19",name:"Matrix Printer",note:"note3",stock:"No", ship:"FedEx",sdate:"2007-12-03"}, 
     {id:"20",name:"Desktop Computer",note:"note",stock:"Yes",ship:"FedEx", sdate:"2007-12-03"}, 
     {id:"21",name:"Laptop",note:"Long text ",stock:"Yes",ship:"InTime",sdate:"2007-12-03"}, 
     {id:"22",name:"LCD Monitor",note:"note3",stock:"Yes",ship:"TNT",sdate:"2007-12-03"}, 
     {id:"23",name:"Speakers",note:"note",stock:"No",ship:"ARAMEX",sdate:"2007-12-03"} 
    ]; 

어떻게 내 데이터가 내있는 jqGrid에 표시 얻을 수 있도록 내가 AJAX 호출 후 " "없이 원래 JSON 형식을 유지합니까?

+0

를 배열을 통해 루프를 필요로하고 추가이기 때문에 당신이 단지 외부 광장 주위에 따옴표에 대해 얘기하는, 입력으로 객체를 기대한다 괄호? 그것을 모방하지 마십시오. 왜 당신은'JSON.stringify (데이터)'를 calilng하고 있습니까? JqGrid에서 실제로'data'를 어떻게 사용하는지 보여주기 위해 질문을 편집하십시오. JqGrid가 JSON (* 문자열 *)을 기대합니까? 아니면 객체를 기대합니까? – nnnnnn

+0

.stringify()를 사용하지 않을 때 [object, Object]가 반환됩니다. –

+0

JqGrid는 Api의 원본 Json Response를 기대합니다. –

답변

0

어쩌면 이렇게 할 수 있을까요?

'[{"MovieId":2,"Title":"End Game","Year"...'.replace(new RegExp('"', 'g'), '') 
+0

이렇게 모두 제거되었습니다. 응답에서 "" "[] 밖에 제거 된 것들이 필요합니다. –

1

당신은 당신의 데이터는 JSON 형식으로 이미() JSON.stringify 필요하지 않습니다

+2

데이터가 이미 JSON 문자열이라고 말하고 있습니까? 왜냐하면 그것은 javscript 객체가 아니기 때문입니다. –

+0

JqGrid는 객체를 기대하고 있으며이를 문자열로 변환하려고합니다. console.log (data); 당신은 올바른 형식을 가지고 있는지 확인하기 위해 – Taki

0

JSON은 기본적으로 문자열로 자바 스크립트를 통과 형식입니다. JQuery는 문자열을 실제로 파싱하여 원하는대로 처리합니다. 그런 다음 객체를 JSON으로 다시 변환합니다. 문자열이지만 실제로 사용하고자하는 것은 객체입니다. TL : DR : 문자열을 변경하지 마십시오

0

JqGrid의 addRowData 메소드를 사용할 수 있습니다. 당신의 결과는 객체의 배열 방금 행 데이터

var data = [{"MovieId":2,"Title":"End Game","Year":"20113","Actors":"Cuba Gooding Jr., Angie Harmon, James Woods, Patrick Fabian","Plot":"A secret Service agent and a news reporter investigate the conspiracy behind the assassination of the President","Director":"Andy Cheng","Cover":"Endgame-movie-cover.jpg"},{"MovieId":3,"Title":"Saving Private Ryan","Year":"1998","Actors":"Tom Hanks, Tom Sizemore, Edward Burns, Barry Pepper","Plot":"Following the Normandy Landings, a group of U.S. soldiers go behind enemy lines to retrieve a paratrooper whose brothers have been killed in action","Director":"Steven Spielberg","Cover":"Saving-Private-Ryan-Cover.jpg"},{"MovieId":4,"Title":"Cobra","Year":"1986","Actors":"Sylvester Stallone, Brigitte Nielsen, Reni Santoni, Andrew Robinson","Plot":"A tough-on-crime street cop must protect the only surviving witness to a strange murderous cult with far reaching plans","Director":"George P. Cosmatos","Cover":"cobra.jpg"},{"MovieId":5,"Title":"Savages","Year":"2012","Actors":"Blake Lively, Taylor Kitsch, Aaron Taylor-Johnson, Jana Banker","Plot":"Pot growers Ben and Chon face off against the Mexican drug cartel who kidnapped their shared girlfriend","Director":"Oliver Stone","Cover":"savages.jpg"},{"MovieId":6,"Title":"The Man in the Iron Mask","Year":"1998","Actors":"Leonardo DiCaprio, Jeremy Irons, John Malkovich, Gérard Depardieu","Plot":"The cruel King Louis XIV of France has a secret twin brother who he keeps imprisoned. Can the twin be substituted for the real king?\"","Director":"Randall Wallac","Cover":"maninimask.jpg"},{"MovieId":7,"Title":"The Shawshank Redemption","Year":"1994","Actors":"Tim Robbins, Morgan Freeman, Bob Gunton, William Sadler","Plot":"Two imprisoned men bond over a number of years, finding solace and eventual redemption through acts of common decency.","Director":"Frank Darabont","Cover":"[email protected]_V1_SX300.jpg"},{"MovieId":8,"Title":"Perfume: The Story of a Murderer","Year":"2006","Actors":"Ben Whishaw, Francesc Albiol, Gonzalo Cunill, Roger Salvany","Plot":"Jean-Baptiste Grenouille, born with a superior olfactory sense, creates the world's finest perfume. His work, however, takes a dark turn as he searches for the ultimate scent.","Director":"Tom Tykwer","Cover":"[email protected]@._V1_SX300.jpg"},{"MovieId":9,"Title":"Catch Me If You Can","Year":"2002","Actors":"Leonardo DiCaprio, Tom Hanks, Christopher Walken, Martin Sheen","Plot":"The true story of Frank Abagnale Jr. who, before his 19th birthday, successfully conned millions of dollars' worth of checks as a Pan Am pilot, doctor, and legal prosecutor.","Director":"Steven Spielberg","Cover":"MV5BMTY5MzYzNjc5NV5BMl5BanBnXkFtZTYwNTUyNTc2._V1_SX300.jpg"},{"MovieId":10,"Title":"James Bond 007","Year":"2003","Actors":"Pierce Brosnan, John Cleese, Willem Dafoe, Judi Dench","Plot":"MI6 agent James Bond:007 rushes into action to rescue Oxford scientist Katya Nadanova from a terrorist facility in Egypt. After rescuing Katya bond heads to a mining town in Peru to ..","Director":"Scot Bayless","Cover":"007.jpg"},{"MovieId":11,"Title":"The Mask","Year":"1994","Actors":"Jim Carrey, Peter Riegert, Peter Greene, Amy Yasbeck","Plot":"Bank clerk Stanley Ipkiss is transformed into a manic superhero when he wears a mysterious mas","Director":"Chuck Russel","Cover":"theMask.jpg"},{"MovieId":12,"Title":"Rambo","Year":"2008","Actors":"Sylvester Stallone, Julie Benz, Matthew Marsden, Graham McTavish","Plot":"In Thailand, John Rambo joins a group of missionaries to venture into war-torn Burma, and rescue a group of Christian aid workers who were kidnapped by the ruthless local infantry unit","Director":"Sylvester Stallone","Cover":"rambo.jpg"},{"MovieId":13,"Title":"The Green Mile","Year":"1999","Actors":"Tom Hanks, David Morse, Michael Clarke Duncan, Bonnie Hunt\",\"Plot\":\"The lives of guards on Death Row are affected by one of their charges: a black man accused of child murder and rape, yet who has a mysterious gift","Plot":"The lives of guards on Death Row are affected by one of their charges: a black man accused of child murder and rape, yet who has a mysterious gift.\",\"Language\":\"English, French\",\"Country\":\"USA\",\"Awards\":\"Nominated for 4 Oscars. Another 15 wins & 30 nominations","Director":"Frank Darabont","Cover":"greenmile.jpg"}] 
 
$("#grid").jqGrid({ 
 
    datatype: "local", 
 
    height: 250, 
 
    colNames: ['MovieId', 'Title', 'Title', 'Actors', 'Plot','Director'], 
 
    colModel: [{ 
 
     name: 'MovieId', 
 
     index: 'MovieId', 
 
     width: 60, 
 
    search:false}, 
 
    { 
 
     name: 'Title', 
 
     index: 'Title', 
 
     width: 120, 
 
    search:false}, 
 
    { 
 
     name: 'Year', 
 
     index: 'Year', 
 
     width: 60, 
 
    search:false}, 
 
    { 
 
     name: 'Actors', 
 
     index: 'Actors', 
 
     width: 120, 
 
    search:false}, 
 
    { 
 
     name: 'Plot', 
 
     index: 'Plot', 
 
     width: 120, 
 
    search:false}, 
 
    { 
 
     name: 'Director', 
 
     index: 'Director', 
 
     width: 120, 
 
     search:false} 
 
    ], 
 
    caption: "IMDB" 
 
}); 
 

 
for (var i = 0; i <= data.length; i++) { 
 
    $("#grid").jqGrid('addRowData', i + 1, data[i]); 
 
}
<link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/themes/base/jquery-ui.css" rel="stylesheet"/> 
 
<link href="https://cdn.jsdelivr.net/free-jqgrid/4.13.5/css/ui.jqgrid.min.css" rel="stylesheet"/> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<script src="https://cdn.jsdelivr.net/free-jqgrid/4.13.5/js/jquery.jqgrid.min.js"></script> 
 

 
<table id="grid"></table>

+0

'JqGrid는 JSON 객체를 기대하고있다.'- 그런 건 없지만, JSON은 문자열이다. –

+0

'addRowData'에서 어디로 가라? –

+0

@FrankOdoom 그것의 jqgrid 방법, 여기에서 확인하십시오 http://www.trirand.net/documentation/php/_2yh0uuou0.htm – Deep