How to pass two array like structure in JSON fetch API

时间:2017-04-06 17:14:33

标签: json react-native fetch-api

This is example code from react native doc..

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

Now I want to post two array like structures or data to url and those parameters are like this below:

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}]",
  })
})

But this is not working ,How to post two things in POST fetch method.While attaching.Please note those 0: and 1: thing describing two seaparate json values

1 个答案:

答案 0 :(得分:1)

问题是您的JSON无效 - 应引用诸如0之类的属性,例如JSON.stringify({" 0":...,然后你有一个引用围绕对象标识符的开头{0:" {" fromCity" ... - >这应该是{" 0":{" fromCity" ...

正确格式化邮件第一部分的示例:

{   " 0":{" fromCity":" Mumbai,IN - Chhatrapati Shivaji International(BOM)",         " toCity":" Goa,IN - Dabolim(GOI)",         "离开":" 09 - 04 - 2017",

和属性1的开始:

" 1":[{" selectedOnwardFlight":[{" lstExtraServices":[]

用于获取JSON排序的好网站是jsonlint.com