[英]JSON processing - Javascript
我有以下包含json的腳本。
$http({
url: '/mpdValidation/mpdValidate',
method: "POST",
data: { 'message' : mpdData }
).then(function(response) {
console.log(response.data.searchResults[0].customers);
$rootScope.mpdValidatedData = response.data.searchResults[0].customers;
var partyAddressesArray = $rootScope.mpdValidatedData['partyAddresses '];
console.log(partyAddressesArray.length);
});
我將在表格中查看。
我擔心的是,此json中的數組很少,我無法迭代/處理。
那是,
我的json :
{
"searchResults": [
{
"id": 1,
"firstName": "kelly",
"lastName": "johnson",
"facets": {
"totalCustomerfound": 118541
},
"customers": [
{
"score": 20,
"fullName": "kelly kelly",
"firstName": "kelly",
"lastName": "kelly",
"dob": "1963/01/22",
"memNum": "42900711",
"phoneList": [
{
"phoneType": "homephonenumber",
"phoneExchange": "222",
"phoneAreaCode": "111",
"phoneNumber": "9198",
"phoneFullNumber": "1112229198"
},
{
"phoneType": "primaryphonenumber",
"phoneExchange": "444",
"phoneAreaCode": "333",
"phoneNumber": "9198",
"phoneFullNumber": "3334449198"
}
],
"partyAdresses": [
{
"addressType": "home",
"address1": "22 IRON KETTLE ST",
"zipCode": "89130-2222",
"city": "LAS VEGAS",
"state": "NV"
},
{
"addressType": "mailing",
"address1": "11 SANDPIPER LN",
"zipCode": "80601-1111",
"city": "BRIGHTON",
"state": "CO"
}
],
"policyList": [
],
"membershipList": [
{
"termEffectiveDate": "2015/12/16",
"termExpirationDate": "2016/12/16",
"contractTransactionTimeStamp": "2017/09/05 19:46:29.722",
"policyInceptionDate": "2015/12/16",
"policyNumber": "112233",
"policyStatus": "INACTIVE",
"prodTypeCode": "MSHP",
"productCode": "MSHP",
"memType": "PREMIER",
"contractAddress": [
],
"roleList": [
{
"roleType": "PRIMARY MEMBER",
"sourceId": "4290011",
"roleStatus": "INACTIVE",
"roleStartDate": "2015/12/16",
"membershipNumber": "4290011"
}
]
}
]
}
]
]
}
我的桌子 :
<table id="mpdRecordTable" st-table="display_mpd_records"
st-safe-src="mpdValidatedData" ng-show="mpdValidatedData"
class="table table-bordered table-striped shadow p-3 mb-5 bg-white rounded" ng-controller="mpdController">
<caption>*MPD - Validated customer returns</caption>
<thead class="thead-dark">
<tr>
<th>First name</th>
<th>Last name</th>
<th>Date of birth</th>
<th>Policy number</th>
<th>Policy status</th>
<th>Address type</th>
<th>Address</th>
<th>Phone number</th>
</tr>
</thead>
<tbody>
<tr st-select-row="row" st-select-mode="multiple" ng-repeat="row in display_mpd_records">
<td ng-bind="row.firstName"></td>
<td ng-bind="row.lastName"></td>
<td ng-bind="row.dob"></td>
<td ng-bind="row.policyNumber"></td>
<td ng-bind="row.policyStatus"></td>
<td ng-bind="row.addressType"></td>
<td ng-bind="row.address"></td>
<td ng-bind="row.phoneNumber"></td>
</tr>
</tbody>
</table>
由於我的json中的partyAddresses是一個數組,因此不會在row.address中顯示。 我需要來自json的家庭addressType的address1,address2,city,state,zipCode作為單個字符串顯示在row.address列中。
同樣,對於phoneNumber,homePhone必須可見。
名字,姓氏將按預期填充,但地址和電話號碼不會填充。 有人可以幫忙嗎?
您可以在特定行中嘗試
ng-repeat
,如下所示
<td>
<div ng-repeat="addr in row.partyAdresses">
{{addr. address1}}, {{addr. address2}} ...so on
<br>
</div>
</td>
對於phoneNumber等,
在控制器內部,您可以使用傳播運算符[... array]在將數組傳遞到視圖之前將其轉換為字符串,有關更多信息,請檢查以下鏈接:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.