2014-09-27 89 views
0

我似乎無法找到這個在線的答案,但我認爲這是因爲我不太確定要搜索什麼,因爲我對xml和json很陌生。json decode獲取屬性

如果我有這樣的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; 
+2

這就是爲什麼有時這種方法不會削減它,它不是一個完美的方法來使用,因爲它不能完美地保存數據,你只需要走它正常的方式 – Ghost 2014-09-27 14:07:42

+0

它也沒有那麼糟糕 - 旁邊有許多重複它現場 - 首先閱讀它在PHP手冊:[基本的SimpleXML用法](http://php.net/manual/en/ simplexml.examples-basic.php) – hakre 2014-09-28 08:38:59

回答

0

對於這種情況,爲什麼不穿越節點正常,並使用->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 
-1

享受解決這個。完全使用命中和試用方法。 我的方法: 我試圖json_encode由PaymentMethodUsed陣列:

$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'];