2017-04-06 71 views
0

這是示例代碼從反應天然DOC ..如何傳遞兩個陣列狀的結構中JSON取API

fetch('https://mywebsite.com/endpoint/', { 
    method: 'POST', 
    headers: { 
    'Accept': 'application/json', 
    'Content-Type': 'application/json', 
    }, 
    body: JSON.stringify({ 
    firstParam: 'yourValue', 
    secondParam: 'yourOtherValue', 
    }) 
}) 

現在我要發佈2個陣列狀結構或數據到URL和這些參數都是這樣如下:

fetch('https://mywebsite.com/endpoint/', { 
    method: 'POST', 
    headers: { 
    'Accept': 'application/json', 
    'Content-Type': 'application/json', 
    }, 
    body: JSON.stringify({ 
    0: "{"fromCity":"Mumbai, IN - Chhatrapati Shivaji International (BOM)","toCity":"Goa, IN - Dabolim (GOI)","depart":"09-04-2017","returndate":"","adult":1,"child":0,"infant":0,"classoftravel":"E","prefferedairline":"AI","trip":"dom","tripType":"one way"}", 
    1: "[{"selectedOnwardFlight":[{"lstExtraServices":[],"flightDuration":"01:15","departuretime":"09-04-2017T13:30:00","arrivalairport":"GOI_Dabolim, Goa","freeBaggageAllowedWeight":25,"segment":1,"departureairport":"BOM_Chhatrapati Shivaji International, Mumbai","mac":"AI_Air India","fno":"663","dpartTerInfo":"2","oac":"AI","arrivaltime":"09-04-2017T14:45:00"}],"selectedReturnFlight":[],"selectedThirdFlight":[],"selectedFourthFlight":[],"selectedFifthFlight":[],"airlineLogoPath":"AI.png","airlineName":"Air India","airlineRecommend":"N","airlinecode":"AI","faretype":"Refundable","flightsID":"0","nonStop":"Y","onduration":[["0:1:15"],["NA"],["0:1:15"],["NA"],"Chhatrapati Shivaji International, Mumbai",0],"reduration":[],"thirdduration":[],"fourthduration":[],"fifthduration":[],"fare":[{"lstExtraServices":[],"ibp":"0","cbp":"0","tp":"6528","pc":"AI","fareBasisDetails":[{"paxType":"ADULT","fareBasisCode":"TAP15B","segment":"1-663","fareType":"Refundable","rbd":"T","breakPoint":"Y"}],"fareClassType":"Economy","taxDetails":{"itaxdetails":[],"ctaxdetails":[],"atax":"496","ctax":"0","ttax":"496","itax":"0","ataxdetails":[{"name":"YR","value":"70"},{"name":"IN","value":"0"},{"name":"WO","value":"150"},{"name":"YM","value":"138"},{"name":"JN","value":"128"},{"name":"xt","value":"10"}]},"abp":"2200"}],"vendor":"amadeus","totalprice":"2713","originaltp":"2696","serviceTax":17}]", 
    }) 
}) 

但是這是行不通的,如何發佈兩件事情在POST取method.While attaching.Please注意那些0:1:描述兩個seaparate JSON的東西值

回答

1

這個問題我您的JSON無效 - 例如0等屬性應引用例如JSON.stringify({「0」:...,那麼你有一個對象標識符{0:「{」fromCity「的開始... - >這應該是{」0「:{」fromCity「 ...

的正確格式化你的消息的第一部分樣本:

{ 「0」:{ 「fromCity」: 「孟買,IN - 孟買國際(BOM)」, 「toCity 「: 」果,IN - Dabolim(GOI)「, 」 出發 「: 」09 - 04 - 2017「,

和財產1的開始:

」1「:[{」 selectedOnwardFlight 「:[{」 lstExtraServices「:[]

的好發部位使用,讓您的JSON排序是jsonlint.com

+0

OKay..I'll嘗試,讓你知道.. – SmitSherlock