How to send complex nested JSON from Angular Typescript to Web Api?

十年热恋 提交于 2019-12-08 12:56:06

问题


I have 2 issues with my json data as I am use to sending it "flat"

Example typescript class model

UserID: number;
AppID: number;
Key: string;
HearingsAndEventsType: number

In the past I would send above like this.

{
   "UserID": 61525,
   "AppID": 15,
   "Key": "abc",
   "HearingsAndEventsType": 1
}

NOT ANYMORE, I have to send as the nested object with 2 changes to the JSON object

  1. "PageQueryString": {...
  2. },   "HearingsAndEventsType": 1

THUS the mandatory structure that I need to send will look INSTEAD like this

{
  "PageQueryString": {
      "UserID": 61525,
      "AppID": 15,
      "Key": "abc"
},
    "HearingsAndEventsType": 1
}

I tried to ask the question in the link below, but I THINK it was too long in length for people to understand what I was needing. Thus other question is pretty much the same thing.... so to the person kind and smart enough to help, 2 questions for the price of 1 . thx

Angular Typescript sending complex json data to web api when model is flat

Essential i use JSON stringify and try and send model over, but I need to other the json

 getPageCommonData(menu: Menu)  {
    return this.http.post(pageCommonData, JSON.stringify(menu), httpOptions)
  ....
 }

回答1:


You just need to create two models:

class QueryString {
    UserID: number;
    AppID: number;
    Key: string;
}

class Menu {
    PageQueryString: QueryString;
    HearingsAndEventsType: number;
}


来源:https://stackoverflow.com/questions/48573409/how-to-send-complex-nested-json-from-angular-typescript-to-web-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!