Angular 4 - 将JSON映射到模型,反之亦然

时间:2017-06-15 03:44:47

标签: json angular typescript

我需要从Angular 4 app调用[POST] REST服务。其余服务需要请求正文中的JSON。

我在Angular中构建的JSON,基于一些用户输入看起来像

<!DOCTYPE html>
<html>
<head>
<style>
body {font-family: "Lato", sans-serif;}

.tablink {
    background-color: #555;
    color: white;
    float: left;
    border: none;
    outline: none;
    cursor: pointer;
    padding: 14px 16px;
    font-size: 17px;
    width: 25%;
}

.tablink:hover {
    background-color: #777;
}

/* Style the tab content */
.tabcontent {
    color: white;
    display: none;
    padding: 50px;
    text-align: center;
}

#London {background-color:red;}
#Paris {background-color:green;}
#Tokyo {background-color:blue;}
#Oslo {background-color:orange;}
.text {
  display: none;
}
.show {
  display: block;
}
</style>
</head>
<body>

<div id="London" class="tabcontent">
  <h3>London</h3>
  <p>London is the capital city of England.</p>
</div>

<div id="Paris" class="tabcontent">
  <h3>Paris</h3>
  <p>Paris is the capital of France.</p> 
</div>

<div id="Tokyo" class="tabcontent">
  <h3>Tokyo</h3>
  <p>Tokyo is the capital of Japan.</p>
</div>

<div id="Oslo" class="tabcontent">
  <h3>Oslo</h3>
  <p>Oslo is the capital of Norway.</p>
</div>

<button class="tablink" onclick="openCity('London', this, 'red')" id="defaultOpen">London</button>
<button class="tablink" onclick="openCity('Paris', this, 'green')">Paris</button>
<button class="tablink" onclick="openCity('Tokyo', this, 'blue')">Tokyo</button>
<button class="tablink" onclick="openCity('Oslo', this, 'orange')">Oslo</button>
<p class="Oslo text">
oslo
</p>
<p class="Paris text">
paris
</p>
<p class="Tokyo text">
tokyo
</p>
<p class="London text show">
london
</p>
<script>
function openCity(cityName,elmnt,color) {
    var i, tabcontent, tablinks;
    tabcontent = document.getElementsByClassName("tabcontent");
    for (i = 0; i < tabcontent.length; i++) {
        tabcontent[i].style.display = "none";
    }
    tablinks = document.getElementsByClassName("tablink");
    for (i = 0; i < tablinks.length; i++) {
        tablinks[i].style.backgroundColor = "";
    }
    document.getElementById(cityName).style.display = "block";
    elmnt.style.backgroundColor = color;
    var texts = document.getElementsByClassName('text');
    for (var i = 0; i < texts.length; i++) {
    	if (texts[i].classList.contains(cityName)) {
      	texts[i].classList.add('show');
      } else {
      	texts[i].classList.remove('show');
      }
    }

}
// Get the element with id="defaultOpen" and click on it
document.getElementById("defaultOpen").click();
</script>
   
</body>
</html> 

在我的代码中,我正在创建这个json

{
    "val-type": "prop",
    "val-name": "prop1",
    "op-ter": "DIFF",
    "value": "Unknown",
    "match-twice": false,
    "case-sensitive": false
}

我希望我可以为这个结构创建一个模型,以确保遵守json结构。所以,我继续创建了一个model.ts文件,如下所示

let jsonSubpart = {
      "val-type": userInput.valtype,
      "val-name": userInput.valname,
      "op-ter": userInput.opter,
      "value": userInput.val,
      "match-twice": false,
      "case-sensitive": false
    }

我的想法是在构建json时使用这个模型 -

export class SubPart {
    public valType: string;
    public valName: string;
    public opTer: string;
    public value: string;
    public matchTwice: boolean = false;
    public caseSensitive: boolean = false;

    constructor(valType: string, valName: string, opTer: string, value: string,
        matchTwice: boolean, caseSensitive: boolean) {
        this.valType=valType;
        this.valName= valName;
        this.opTer=opTer;
        this.value = value;
        this.matchTwice=matchTwice;
        this.caseSensitive = caseSensitive;
    }
}   

但是,这不起作用,因为json和类中的字段名称不匹配。因此,angular不会知道val-type与valType相同。我不能将.ts文件中的变量名保存为val-type,因为它不是有效的变量名,因为' - '。

想听听专家们在这种情况下最好的方法是什么?我应该在不担心课程的情况下构建json,还是有另一种方法来获得这种强类型?

1 个答案:

答案 0 :(得分:3)

发布时,您可以使用Json typescript decorater序列化模型。

例如,像往常一样声明类:

export class SubPart {
    @JsonProperty('val-type')
    public valType: string;
}

填写模型

  let jsonSubpart: Subpart = {
     valType: userInput.valtype
  }

并在发布模型时

 var json = serialize(jsonSubpart);