我使用Stripe付款将一张发票清单返回给我的AngularJS应用程序。
我得到以下结果:
Stripe\Collection JSON: {
"object": "list",
"data": [
{
"id": "in_1BKR1lK9EsaSH2dAtMQ4JF1K",
"object": "invoice",
"amount_due": 113,
"application_fee": null,
"attempt_count": 1,
"attempted": true,
"billing": "charge_automatically",
"charge": "ch_1BKRxsK9EsaSH2dA0xW34Qsr",
"closed": true,
"currency": "gbp",
"customer": "cus_9ry4RT082N7T7N",
"date": 1509799533,
"description": null,
"discount": null,
"ending_balance": 0,
"forgiven": false,
"lines": {
"object": "list",
"data": [
{
"id": "ii_1BJm2pK9EsaSH2dA6t4ytmm6",
"object": "line_item",
"amount": 6,
"currency": "gbp",
"description": "Remaining time on per act monthly after 02 Nov 2017",
......等
如何遍历此JSON以列出每个发票对象?
我试过了:
$scope.myUserData.invoices = result.data; (returns above JSON)
$scope.myUserData.invoices = result.data[0]; (returns "S")
$scope.myUserData.invoices = result.data.object; (returns blank)
$scope.myUserData.invoices = result.data.data; (returns blank)
由于你的result.data[0]
返回字符'S',你的result
对象很可能是一个字符串。所以你可能会尝试像这样使用JSON.parse()
:
var json = JSON.parse(result);
console.log(json.data[0]);
编辑:使用OP的观察结果进行调整 你的结果对象在路上的某个地方是字符串化的(并且首先应该是一个json对象),然后使用
$scope.myUserData.invoices = angular.fromJson(result.data);
应该管用。
也许会有所帮助
let x = 'Stripe\Collection JSON: {hello:"world"}';//Should be your result instead this string
x = x.replace("Stripe\Collection JSON: ", "");
x = JSON.stringify(x)
x = JSON.parse(x);
console.log(x)
只需输入您的结果值即可。然后你应该有一个JSON对象,并执行此结果.data [0]
爱德华给出的答案略有改变,反斜杠需要逃脱。
这允许我只在Typescript中得到结果,也在Angular中工作:
let x = result._body;
x = x.replace("Stripe\\Collection JSON: ", "");
x = JSON.parse(x);
x.data.forEach(element => {
console.log('card JSON: ' + JSON.stringify(element));
console.log('card id: ' + element.id);
});