【发布时间】:2019-11-26 02:56:28
【问题描述】:
对不起,如果这是一个愚蠢的问题,我只是感觉迷失在这里。我正在尝试将某些值从我的 XML 文件中获取到我的普通 PHP 文件中的 JSON 数组中。
这就是我收集 XML 文件数据的方式以及我的 JSON 的显示方式,具体取决于哪种情况为真:
switch ($creditId) {
case 123:
$xml = new XMLReader;
$xml->open('123.xml');
$doc = new DOMDocument;
while ($xml->read() && $xml->name !== 'Decision');
while ($xml->name === 'Decision')
{
$node = simplexml_import_dom($doc->importNode($xml->expand(),
true));
$reason_text = $node->Reason->ReasonText ;
echo "$reason_text \n";
// go to next <DecisionResponse>
$xml->next('Decision');
}
echo '[{
"creditId": 123,
"client": "Peter",
"Decision": "Solo" ,
"Factors": ["REASONTEXT1","REASONTEXT2"]
}]';
break;
case 789:
$xml = new XMLReader;
$xml->open('789.xml');
$doc = new DOMDocument;
while ($xml->read() && $xml->name !== 'Decision');
while ($xml->name === 'Decision')
{
$node = simplexml_import_dom($doc->importNode($xml->expand(),
true));
$reason_text = $node->Reason->ReasonText ;
echo "$reason_text \n";
// go to next <DecisionResponse>
$xml->next('Decision');
}
echo '[{
"creditId": 789,
"client": "Jonas",
"Decision": "Random",
"Factors": ["REASONTEXT1","REASONTEXT2"]
}]';
break;
default:
http_response_code(404);
}
REASONTEXT1 和 REASONTEXT2 是变量 $reason_text 的值应显示在 JSON 数组中的位置。例如:我的 123 案例的 json 显示现在看起来像这样($reason_text 的回声输出值 RandomReason1 和 RandomReason2):
RandomReason1
RandomReason2
[{
"creditId": 123,
"client": "Peter",
"Decision": "Solo",
"Factors": ["REASONTEXT1","REASONTEXT2"]
}]
我希望这些值显示如下:
[{
"creditId": 123,
"client": "Peter",
"Decision": "Solo",
"Factors": ["RandomReason1","RandomReason2"]
}]
最后,我想将某些 XML 数据插入 JSON 数组。提前谢谢!
【问题讨论】:
标签: javascript json vue.js