【问题标题】:json decode get attribute [duplicate]json解码获取属性[重复]
【发布时间】:2014-09-27 14:05:20
【问题描述】:

我似乎无法在网上找到答案,但我认为这是因为我不太确定要搜索什么,因为我对 xml 和 json 非常陌生。

如果我有这个 xml

$postData = '<PaymentNotification Stage="false">
  <MerchantReference>100004117</MerchantReference>
  <TransactionType>PAYMENT</TransactionType>
  <TransactionState>SUCCESSFUL</TransactionState>
  <ResultCode>00</ResultCode>
  <ResultMessage>Successful</ResultMessage>
  <PayUReference>1167986976014</PayUReference>
  <Basket>
    <Description>Store Order Number:100004117</Description>
    <AmountInCents>100</AmountInCents>
    <CurrencyCode>ZAR</CurrencyCode>
    <Products/>
  </Basket>
  <PaymentMethodsUsed>
    <Eft BankName="ABSA" AmountInCents="100" Reference="CUMVSIUPFG" AccountNumber="4077920871" BranchNumber="632005" AccountType="Cheque" TimeLimit="168" Currency="ZAR"/>
  </PaymentMethodsUsed>
  <IpnExtraInfo>
    <ResponseHash></ResponseHash>
  </IpnExtraInfo>
</PaymentNotification>';

然后json解码

$returnData = json_decode(json_encode(simplexml_load_string($postData)),true);

我可以检索对象,例如。

$cost_amount = $returnData['Basket']['AmountInCents'];

但是我如何获得 Eft 中的属性,例如。银行名称?

我试过这些都没有成功

$paid_amount = $returnData['PaymentMethodsUsed']['CreditCard']['@AmountInCents'];
and
$paid_amount = $returnData[0]->PaymentMethodsUsed[0]->CreditCard->AmountInCents;

【问题讨论】:

  • 这就是为什么有时这种方法不能削减它,它不是一个完美的方法,因为它不能完美地保存数据,你只需要以正常的方式遍历它
  • 它也不是那么糟糕 - 除了现场的许多重复 - 首先在 PHP 手册中阅读它:Basic SimpleXML usage

标签: php xml json


【解决方案1】:

对于这种情况,为什么不正常遍历节点,使用-&gt;attributes()方法:

$postData = '<PaymentNotification Stage="false">
  <MerchantReference>100004117</MerchantReference>
  <TransactionType>PAYMENT</TransactionType>
  <TransactionState>SUCCESSFUL</TransactionState>
  <ResultCode>00</ResultCode>
  <ResultMessage>Successful</ResultMessage>
  <PayUReference>1167986976014</PayUReference>
  <Basket>
    <Description>Store Order Number:100004117</Description>
    <AmountInCents>100</AmountInCents>
    <CurrencyCode>ZAR</CurrencyCode>
    <Products/>
  </Basket>
  <PaymentMethodsUsed>
    <Eft BankName="ABSA" AmountInCents="100" Reference="CUMVSIUPFG" AccountNumber="4077920871" BranchNumber="632005" AccountType="Cheque" TimeLimit="168" Currency="ZAR"/>
  </PaymentMethodsUsed>
  <IpnExtraInfo>
    <ResponseHash></ResponseHash>
  </IpnExtraInfo>
</PaymentNotification>';

$xml = simplexml_load_string($postData);

$cents = (string) $xml->PaymentMethodsUsed->Eft->attributes()->AmountInCents;
echo $cents; // 100

【讨论】:

    【解决方案2】:

    很高兴解决这个问题。完全使用了命中和审判的方法。 我的方法: 我尝试通过以下方式对 PaymentMethodUsed 数组进行 json_encode:

     $x = $returnData['PaymentMethodsUsed'];
     echo json_encode($x);
    

    结果是:

    {"Eft":{"@attributes":{"BankName":"ABSA","AmountInCents":"100","Reference":"CUMVSIUPFG","AccountNumber":"4077920871","BranchNumber":"632005","AccountType":"Cheque","TimeLimit":"168","Currency":"ZAR"}}}
    

    所以,猜到了方法,我希望你能得到你想要的价值:

    echo $returnData['PaymentMethodsUsed']['Eft']['@attributes']['BankName'];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-02
      • 2013-04-05
      • 2019-05-12
      • 1970-01-01
      • 2014-05-07
      • 2018-11-18
      • 1970-01-01
      相关资源
      最近更新 更多