Handlebars.js / JSON Parse:从json解析的数据不按顺序排列

时间:2017-10-20 19:40:06

标签: javascript html json handlebars.js

我有json数据,我通过handlebars.js解析它 但是我得到的输出与json中的输出顺序不完全相同。 我的Json代码:

"RiskModel":{  
         "RiskModel":[  
            {  
                  "ScoreFactors":{  
                  "34":"Total of all balances on bankcard or revolving accounts is too high",
                  "12":"The date that you opened your oldest account is too recent",
                  "39":"Available credit on your open bankcard or revolving accounts is too low",
                  "04":"The balances on your accounts are too high compared to loan amounts"
               }
            }
         ]
      }

我的HTML代码:

{{#arf.RiskModel.RiskModel}}
{{#each ScoreFactors}}
 <li>{{this}} </li>
    {{/each }}
{{/arf.RiskModel.RiskModel}}

我的输出如下:

THE DATE THAT YOU OPENED YOUR OLDEST ACCOUNT IS TOO RECENT
TOTAL OF ALL BALANCES ON BANKCARD OR REVOLVING ACCOUNTS IS TOO HIGH
AVAILABLE CREDIT ON YOUR OPEN BANKCARD OR REVOLVING ACCOUNTS IS TOO LOW
THE BALANCES ON YOUR ACCOUNTS ARE TOO HIGH COMPARED TO LOAN AMOUNTS

&#34; 12&#34;值首先显示然后&#34; 34&#34;值。

我不知道为什么。

我可以知道原因吗?

我该如何解决这个问题?

按照建议转换为数组后编辑: 我和之前的输出相同。 output after converting obj into array

1 个答案:

答案 0 :(得分:0)

ScoreFactors是一个javascript对象,有四个属性&#34; 34&#34;,&#34; 12&#34;,&#34; 39&#34;,&#34; 04&#34;。 javascript对象中的属性没有任何顺序,因此当用每个对象迭代对象时,其他人甚至可能得到四个句子中的另一个顺序。

  • 您需要使用一个数组,以保持订单的完整性:

    &#34; ScoreFactors&#34;:[
              &#34;银行卡或周转帐户的所有余额总额过高&#34;,           &#34;您打开最早的帐户的日期太近了&#34;,           &#34;您的开放式银行卡或循环帐户的可用信用额度太低&#34;,           &#34;与贷款金额相比,您帐户的余额过高&#34;        ]

  • 如果您还需要数字值,则必须使用数组中的对象:

    &#34; ScoreFactors&#34;:[
              {&#34; num&#34;:&#34; 34&#34;,&#34; text&#34;:&#34;银行卡或周转帐户的所有余额总额过高&#34; },           {&#34; num&#34;:&#34; 12&#34;,&#34; text&#34;:&#34;您打开最早的帐户的日期太近了#34; },           {&#34; num&#34;:&#34; 39&#34;,&#34; text&#34;:&#34;您的开放式银行卡或循环帐户的可用信用额太低&#34; },           {&#34; num&#34;:&#34; 04&#34;,&#34; text&#34;:&#34;与贷款金额相比,您帐户的余额过高&#34; }        ]

    <li>{{num}} {{text}} </li>