【问题标题】:JSON processing - JavascriptJSON 处理 - Javascript
【发布时间】:2019-02-28 22:44:20
【问题描述】:

我有以下包含 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 中家庭地址类型的地址 1、地址 2、城市、州、邮政编码作为单个字符串显示在 row.address 列中。

phoneNumber 也一样,homePhone 必须是可见的。

按预期填充名字、姓氏,但未填充地址和电话号码。有人可以帮忙吗?

【问题讨论】:

    标签: javascript arrays angularjs json angular


    【解决方案1】:

    你可以在特定的行尝试ng-repeat,像这样

        <td>
         <div ng-repeat="addr in row.partyAdresses"> 
             {{addr. address1}}, {{addr. address2}} ...so on 
          <br>
         </div>
        </td>
    

    phoneNumber 等类似,

    【讨论】:

    • 显示错误:[ngRepeat:iexp] collection_[track by _id] 中的“item”形式的预期表达式,但得到“addr of row.partyAdresses'。
    • 对不起,我编辑了我的答案。它在 row.partyAddresses 中的地址
    • Np,谢谢你! :)
    【解决方案2】:

    在您的控制器内部,您可以使用扩展运算符 [...array] 将数组转换为字符串,然后再将其传递给您的视图,有关更多信息,请查看以下链接:

    Spread Operator Doc on MDN

    【讨论】:

      猜你喜欢
      • 2012-04-29
      • 1970-01-01
      • 2018-10-14
      • 2010-12-25
      • 2013-04-23
      • 1970-01-01
      • 1970-01-01
      • 2016-09-02
      • 1970-01-01
      相关资源
      最近更新 更多