Angular http.post嵌套json

时间:2018-01-24 10:57:12

标签: json angular symfony angular5

我正在使用Angular5和Symfony 3.4与FOSUserBundle。

我有类似的东西:

register(username: string, email: string, first: string, second: string)
{
  return this.appHttp.post('register', {username, email, first, second}).subscribe(
    data => {
       this.appHttp.logRequest(data);
    }
  );
}

我需要发送给API的是一个格式化的json,如:

{
  "email": "email@test.com",
  "username" : "userTest",
  "plainPassword" : {
    "first" : "123",
    "second": "123"
   }
}

目前,我的json看起来像:

{
  "email": "email@test.com",
  "username" : "userTest",
  "first": "123",
  "second": "123",
}

我如何发布所需的json?

谢谢!

1 个答案:

答案 0 :(得分:5)

只需改变:

def fix_date(entry):
  if entry.startswith('Q'):
    return entry[:2] + ' 20{}'.format(entry[2:])
  elif '-' in entry:
    a, b = entry.split('-')
    return a + ' 20{}'.format(b)
  elif 'Win' in entry:
    a, b = entry.split(' ')
    b= int(b)+1
    b= str(b)
    return a + ' 20{}'.format(b)
  elif ' ' in entry:
    a, b = entry.split(' ')
    return a + ' 20{}'.format(b)
  else:
    return 'Year {}'.format(entry)

dates = [fix_date(x) for x in dates]

order = ['Jan', 'Feb', 'Mar', 'Q1', 'Win', 'Apr', 'May', 'Jun', 'Q2', 'Jul', 'Aug', 'Sep', 'Q3', 'Sum', 'Oct', 'Nov', 'Dec', 'Q4', 'Year']

dates_partial = [' '.join(pair) for pair in sorted([date.split() for date in dates], key=lambda x: (x[1], order.index(x[0])))]

def revert_date(entry):
  if entry.startswith('Q'):
    a, b = entry.split(' ')
    return a + b[2:]
  elif entry.startswith('Y'):
    a, b = entry.split(' ')
    return b
  elif 'Sum' in entry:
    a, b = entry.split(' ')
    return 'Sum' + b[2:]
  elif 'Win' in entry:
    a, b = entry.split(' ')
    b= int(b)-1
    b= str(b)
    return 'Win' + b[2:]
  else:
    a, b = entry.split(' ')
    return a + '-' +b[2:]

dates = [revert_date(x) for x in dates_partial]

{username, email, first, second}