Json Path工作不正常

时间:2015-05-10 06:42:41

标签: javascript json path encode

我有json消息,我必须从中检索两个不同的值。我使用json路径,但它没有检索任何响应。以下是json消息。

{
    "ContractAgreements": {
        "ContractAgreement": [
            {
                "Code": null,
                "ConnectionPoint": {
                    "Code": null,
                    "StartDate": null
                },
                "ContractedProducts": null
            },
            {
                "Code": 123,
                "ConnectionPoint": {
                    "Code": null,
                    "StartDate": null
                },
                "ContractedProducts": {
                    "ContractedProduct": {
                        "Code": "abc",
                        "ContractedOneOffCharges": {
                            "ContractedOneOffCharge": {
                                "ContractAgreementCode": 123,
                                "Charge": {
                                    "Code": "tekenbonus",
                                    "LanguageDescriptions": {
                                        "LanguageDescription": [
                                            {
                                                "Description": null
                                            },
                                            {
                                                "Description": null
                                            },
                                            {
                                                "Description": null
                                            },
                                            {
                                                "Description": null
                                            },
                                            {
                                                "Description": null
                                            },
                                            {
                                                "Description": null
                                            },
                                            {
                                                "Description": null
                                            },
                                            {
                                                "Description": null
                                            },
                                            {
                                                "Description": "teken bonus"
                                            },
                                            {
                                                "Description": "Signing Fee"
                                            }
                                        ]
                                    }
                                },
                                "OneOffChargeCode": "START",
                                "Status": "INACTIVE",
                                "Criteria1": null,
                                "Criteria2": null,
                                "Criteria3": null,
                                "AmountWithoutTax": null,
                                "OperationDate": null
                            }
                        },
                        "ContractedCharges": null,
                        "ContractedUsageCharges": null,
                        "TerminationReasonCode": null
                    }
                }
            },
            {
                "Code": 1234567,
                "LocationCode": 123,
                "Description": "test",
                "PackageCode": "abc",
                "StartDate": "2015-05-02T00:00:00Z",
                "Status": null,
                "EndDate": null,
                "CustomFields": null,
                "ConnectionPoint": {
                    "Code": null,
                    "StartDate": null
                },
                "ContractedProducts": {
                    "ContractedProduct": [
                        {
                            "Code": "DirectDebet",
                            "ContractAgreementCode": 123,
                            "Status": "TERMINATED",
                            "StartDate": "2015-05-02T00:00:00Z",
                            "EndDate": null,
                            "Quantity": 1,
                            "ContractedOneOffCharges": null,
                            "ContractedRecurringCharges": null,
                            "ContractedUsageCharges": null,
                            "TerminationReasonCode": null
                        },
                        {
                            "Code": "abc Contract",
                            "ContractAgreementCode": 1234567,
                            "Status": "INACTIVE",
                            "StartDate": "2015-05-02T00:00:00Z",
                            "EndDate": null,
                            "Quantity": 1,
                            "ContractedOneOffCharges": null,
                            "ContractedRecurringCharges": null,
                            "ContractedUsageCharges": null,
                            "TerminationReasonCode": null
                        }
                    ]
                }
            },
            {
                "Code": "abc",
                "LocationCode": 123,
                "Description": "abc",
                "PackageCode": "abc",
                "StartDate": "2015-04-23T00:00:00Z",
                "Status": null,
                "EndDate": null,
                "CustomFields": null,
                "ConnectionPoint": {
                    "Code": 871687940005297500,
                    "StartDate": "2015-04-23T00:00:00Z"
                },
                "ContractedProducts": {
                    "ContractedProduct": [
                        {
                            "Code": "No",
                            "ContractAgreementCode": "abc",
                            "Status": "TERMINATED",
                            "StartDate": "2015-04-23T00:00:00Z",
                            "EndDate": null,
                            "Quantity": 1,
                            "ContractedOneOffCharges": null,
                            "ContractedRecurringCharges": null,
                            "ContractedUsageCharges": null,
                            "TerminationReasonCode": null
                        },
                        {
                            "Code": "abc",
                            "ContractAgreementCode": "abc",
                            "Status": "TERMINATED",
                            "StartDate": "2015-04-23T00:00:00Z",
                            "EndDate": null,
                            "Quantity": 1,
                            "ContractedOneOffCharges": null,
                            "ContractedRecurringCharges": null,
                            "ContractedUsageCharges": null,
                            "TerminationReasonCode": null
                        },
                        {
                            "Code": "abc",
                            "ContractAgreementCode": "abc",
                            "Status": "TERMINATED",
                            "StartDate": "2015-04-28T00:00:00Z",
                            "EndDate": null,
                            "Quantity": 1,
                            "ContractedOneOffCharges": null,
                            "ContractedRecurringCharges": null,
                            "ContractedUsageCharges": {
                                "ContractedUsageCharge": {
                                    "ContractAgreementCode": "abc",
                                    "Charge": {
                                        "Code": "Elektra High",
                                        "Description": "xyz",
                                        "InvoiceSubCategoryCode": "abc",
                                        "Disabled": false,
                                        "AmountEditable": true,
                                        "LanguageDescriptions": {
                                            "LanguageDescription": [
                                                {
                                                    "Code": "abc",
                                                    "Description": null
                                                },
                                                {
                                                    "Code": "abc",
                                                    "Description": null
                                                },
                                                {
                                                    "Code": "abc",
                                                    "Description": null
                                                },
                                                {
                                                    "Code": "abc",
                                                    "Description": null
                                                },
                                                {
                                                    "Code": "abc",
                                                    "Description": null
                                                },
                                                {
                                                    "Code": "abc",
                                                    "Description": null
                                                },
                                                {
                                                    "Code": "abc",
                                                    "Description": null
                                                },
                                                {
                                                    "Code": "abc",
                                                    "Description": null
                                                },
                                                {
                                                    "Code": "abc",
                                                    "Description": "Elektra Hoog"
                                                },
                                                {
                                                    "Code": "abc",
                                                    "Description": null
                                                }
                                            ]
                                        }
                                    },
                                    "Status": "TERMINATED",
                                    "Criteria1": null,
                                    "Criteria2": null,
                                    "Criteria3": null,
                                    "AmountWithoutTax": null,
                                    "CounterCode": "abc1"
                                }
                            },
                            "TerminationReasonCode": null
                        },
                        {
                            "Code": "abc",
                            "ContractAgreementCode": "abc",
                            "Status": "INACTIVE",
                            "StartDate": "2015-04-23T00:00:00Z",
                            "EndDate": null,
                            "Quantity": 1,
                            "ContractedOneOffCharges": null,
                            "ContractedRecurringCharges": {
                                "ContractedRecurringCharge": {
                                    "ContractAgreementCode": "abc",
                                    "Charge": {
                                        "Code": "abc",
                                        "Description": "abc",
                                        "InvoiceSubCategoryCode": "abc",
                                        "Disabled": false,
                                        "AmountEditable": true,
                                        "LanguageDescriptions": {
                                            "LanguageDescription": [
                                                {
                                                    "Code": "abc",
                                                    "Description": null
                                                },
                                                {
                                                    "Code": "abc",
                                                    "Description": null
                                                },
                                                {
                                                    "Code": "abc",
                                                    "Description": null
                                                },
                                                {
                                                    "Code": "abc",
                                                    "Description": null
                                                },
                                                {
                                                    "Code": "abc",
                                                    "Description": "Elektra Capaciteits Tarief"
                                                },
                                                {
                                                    "Code": "abc",
                                                    "Description": null
                                                },
                                                {
                                                    "Code": "abc",
                                                    "Description": null
                                                },
                                                {
                                                    "Code": "abc",
                                                    "Description": "xyz"
                                                },
                                                {
                                                    "Code": "abc",
                                                    "Description": null
                                                },
                                                {
                                                    "Code": "abc",
                                                    "Description": null
                                                }
                                            ]
                                        }
                                    },
                                    "Status": "INACTIVE",
                                    "Criteria1": null,
                                    "Criteria2": null,
                                    "Criteria3": null,
                                    "StartDate": 1429747200000,
                                    "AmountWithoutTax": null,
                                    "OperationDate": "2015-04-23",
                                    "NextChargeDate": null
                                }
                            },
                            "ContractedUsageCharges": null,
                            "TerminationReasonCode": null
                        }
                    ]
                }
            }
        ]
    }
}

我必须检索"代码":" Elektra High"从它和json路径如下。

ContractAgreements.ContractAgreement[2].Charge.Code

但它没有检索任何东西。 需要帮助。

1 个答案:

答案 0 :(得分:1)

试试php

<?php
$datas = '{
    "ContractAgreements": {
        "ContractAgreement": [
            {
                "Code": null,
                "ConnectionPoint": {
                    "Code": null,
                    "StartDate": null
                },
                "ContractedProducts": null
            },
            {
                "Code": 123,
                "ConnectionPoint": {
                    "Code": null,
                    "StartDate": null
                },
                "ContractedProducts": {
                    "ContractedProduct": {
                        "Code": "abc",
                        "ContractedOneOffCharges": {
                            "ContractedOneOffCharge": {
                                "ContractAgreementCode": 123,
                                "Charge": {
                                    "Code": "tekenbonus",
                                    "LanguageDescriptions": {
                                        "LanguageDescription": [
                                            {
                                                "Description": null
                                            },
                                            {
                                                "Description": null
                                            },
                                            {
                                                "Description": null
                                            },
                                            {
                                                "Description": null
                                            },
                                            {
                                                "Description": null
                                            },
                                            {
                                                "Description": null
                                            },
                                            {
                                                "Description": null
                                            },
                                            {
                                                "Description": null
                                            },
                                            {
                                                "Description": "teken bonus"
                                            },
                                            {
                                                "Description": "Signing Fee"
                                            }
                                        ]
                                    }
                                },
                                "OneOffChargeCode": "START",
                                "Status": "INACTIVE",
                                "Criteria1": null,
                                "Criteria2": null,
                                "Criteria3": null,
                                "AmountWithoutTax": null,
                                "OperationDate": null
                            }
                        },
                        "ContractedCharges": null,
                        "ContractedUsageCharges": null,
                        "TerminationReasonCode": null
                    }
                }
            },
            {
                "Code": 1234567,
                "LocationCode": 123,
                "Description": "test",
                "PackageCode": "abc",
                "StartDate": "2015-05-02T00:00:00Z",
                "Status": null,
                "EndDate": null,
                "CustomFields": null,
                "ConnectionPoint": {
                    "Code": null,
                    "StartDate": null
                },
                "ContractedProducts": {
                    "ContractedProduct": [
                        {
                            "Code": "DirectDebet",
                            "ContractAgreementCode": 123,
                            "Status": "TERMINATED",
                            "StartDate": "2015-05-02T00:00:00Z",
                            "EndDate": null,
                            "Quantity": 1,
                            "ContractedOneOffCharges": null,
                            "ContractedRecurringCharges": null,
                            "ContractedUsageCharges": null,
                            "TerminationReasonCode": null
                        },
                        {
                            "Code": "abc Contract",
                            "ContractAgreementCode": 1234567,
                            "Status": "INACTIVE",
                            "StartDate": "2015-05-02T00:00:00Z",
                            "EndDate": null,
                            "Quantity": 1,
                            "ContractedOneOffCharges": null,
                            "ContractedRecurringCharges": null,
                            "ContractedUsageCharges": null,
                            "TerminationReasonCode": null
                        }
                    ]
                }
            },
            {
                "Code": "abc",
                "LocationCode": 123,
                "Description": "abc",
                "PackageCode": "abc",
                "StartDate": "2015-04-23T00:00:00Z",
                "Status": null,
                "EndDate": null,
                "CustomFields": null,
                "ConnectionPoint": {
                    "Code": 871687940005297500,
                    "StartDate": "2015-04-23T00:00:00Z"
                },
                "ContractedProducts": {
                    "ContractedProduct": [
                        {
                            "Code": "No",
                            "ContractAgreementCode": "abc",
                            "Status": "TERMINATED",
                            "StartDate": "2015-04-23T00:00:00Z",
                            "EndDate": null,
                            "Quantity": 1,
                            "ContractedOneOffCharges": null,
                            "ContractedRecurringCharges": null,
                            "ContractedUsageCharges": null,
                            "TerminationReasonCode": null
                        },
                        {
                            "Code": "abc",
                            "ContractAgreementCode": "abc",
                            "Status": "TERMINATED",
                            "StartDate": "2015-04-23T00:00:00Z",
                            "EndDate": null,
                            "Quantity": 1,
                            "ContractedOneOffCharges": null,
                            "ContractedRecurringCharges": null,
                            "ContractedUsageCharges": null,
                            "TerminationReasonCode": null
                        },
                        {
                            "Code": "abc",
                            "ContractAgreementCode": "abc",
                            "Status": "TERMINATED",
                            "StartDate": "2015-04-28T00:00:00Z",
                            "EndDate": null,
                            "Quantity": 1,
                            "ContractedOneOffCharges": null,
                            "ContractedRecurringCharges": null,
                            "ContractedUsageCharges": {
                                "ContractedUsageCharge": {
                                    "ContractAgreementCode": "abc",
                                    "Charge": {
                                        "Code": "Elektra High",
                                        "Description": "xyz",
                                        "InvoiceSubCategoryCode": "abc",
                                        "Disabled": false,
                                        "AmountEditable": true,
                                        "LanguageDescriptions": {
                                            "LanguageDescription": [
                                                {
                                                    "Code": "abc",
                                                    "Description": null
                                                },
                                                {
                                                    "Code": "abc",
                                                    "Description": null
                                                },
                                                {
                                                    "Code": "abc",
                                                    "Description": null
                                                },
                                                {
                                                    "Code": "abc",
                                                    "Description": null
                                                },
                                                {
                                                    "Code": "abc",
                                                    "Description": null
                                                },
                                                {
                                                    "Code": "abc",
                                                    "Description": null
                                                },
                                                {
                                                    "Code": "abc",
                                                    "Description": null
                                                },
                                                {
                                                    "Code": "abc",
                                                    "Description": null
                                                },
                                                {
                                                    "Code": "abc",
                                                    "Description": "Elektra Hoog"
                                                },
                                                {
                                                    "Code": "abc",
                                                    "Description": null
                                                }
                                            ]
                                        }
                                    },
                                    "Status": "TERMINATED",
                                    "Criteria1": null,
                                    "Criteria2": null,
                                    "Criteria3": null,
                                    "AmountWithoutTax": null,
                                    "CounterCode": "abc1"
                                }
                            },
                            "TerminationReasonCode": null
                        },
                        {
                            "Code": "abc",
                            "ContractAgreementCode": "abc",
                            "Status": "INACTIVE",
                            "StartDate": "2015-04-23T00:00:00Z",
                            "EndDate": null,
                            "Quantity": 1,
                            "ContractedOneOffCharges": null,
                            "ContractedRecurringCharges": {
                                "ContractedRecurringCharge": {
                                    "ContractAgreementCode": "abc",
                                    "Charge": {
                                        "Code": "abc",
                                        "Description": "abc",
                                        "InvoiceSubCategoryCode": "abc",
                                        "Disabled": false,
                                        "AmountEditable": true,
                                        "LanguageDescriptions": {
                                            "LanguageDescription": [
                                                {
                                                    "Code": "abc",
                                                    "Description": null
                                                },
                                                {
                                                    "Code": "abc",
                                                    "Description": null
                                                },
                                                {
                                                    "Code": "abc",
                                                    "Description": null
                                                },
                                                {
                                                    "Code": "abc",
                                                    "Description": null
                                                },
                                                {
                                                    "Code": "abc",
                                                    "Description": "Elektra Capaciteits Tarief"
                                                },
                                                {
                                                    "Code": "abc",
                                                    "Description": null
                                                },
                                                {
                                                    "Code": "abc",
                                                    "Description": null
                                                },
                                                {
                                                    "Code": "abc",
                                                    "Description": "xyz"
                                                },
                                                {
                                                    "Code": "abc",
                                                    "Description": null
                                                },
                                                {
                                                    "Code": "abc",
                                                    "Description": null
                                                }
                                            ]
                                        }
                                    },
                                    "Status": "INACTIVE",
                                    "Criteria1": null,
                                    "Criteria2": null,
                                    "Criteria3": null,
                                    "StartDate": 1429747200000,
                                    "AmountWithoutTax": null,
                                    "OperationDate": "2015-04-23",
                                    "NextChargeDate": null
                                }
                            },
                            "ContractedUsageCharges": null,
                            "TerminationReasonCode": null
                        }
                    ]
                }
            }
        ]
    }
}';
$datas = json_decode($datas);
echo '<pre>';
print_r($datas->ContractAgreements->ContractAgreement[3]->ContractedProducts->ContractedProduct[2]->ContractedUsageCharges->ContractedUsageCharge->Charge->Code);

应该是js

data.ContractAgreements.ContractAgreement[3].ContractedProducts.ContractedProduct[2].ContractedUsageCharges.ContractedUsageCharge.Charge.Code

结果是&#34; Elektra High&#34;