为什么Json_encode返回一个空数组?

时间:2019-07-18 10:56:46

标签: php json

这是我正在使用的json:

{
    "data": [{
        "Contact_Name": {
            "id": "1884053000034678068"
        },
        "Subject": "1 - XKBKNABJK",
        "Status": "Sans Impact Stock",
        "Entity": "",
        "Etat": "CM - Annul\u00e9",
        "Invoice_Date": "2017-11-21",
        "Discount": 0.000000,
        "Tax": ["TVA"],
        "Total_HT": "55.000000",
        "Total_TVA": 0,
        "Methode_de_paiement": "Payment by check",
        "ID_Lip": "1",
        "Origine": "CM",
        "Type_de_Facture": "Solde",
        "Transporteur": "My carrier",
        "Reference_Colis": "",
        "Billing_Street": "16, Main street",
        "Compl_ment_Facturation": "2nd floor",
        "Billing_City": "Miami",
        "Billing_Country": "\u00c9tats-Unis",
        "Billing_Code": "33133",
        "Shipping_Street": "16, Main street",
        "Compl_ment_Livraison": "2nd floor",
        "Shipping_City": "Miami",
        "Shipping_Country": "\u00c9tats-Unis",
        "Shipping_Code": "33133",
        "Product_Details": [{
            "product": {
                "id": "1884053000030235273"
            },
            "Product_Name": Blouse - Color: White,
             Size: M,
            "list_price": 22.5,
            "quantity": 1,
            "Discount": 0,
            "Discount_Amount": 0,
                "line_tax": [{
                "percentage": 20,
                "name": "TVA"
            }]
        }, {
            "product": {
                "id": "1884053000030235273"
            },
            "Product_Name": Printed Dress - Color: Orange,
            Size: S,
            "list_price": 21.67,
            "quantity": 1,
            "Discount": 0,
            "Discount_Amount": 0,
            "line_tax": [{
                "percentage": 20,
                "name": "TVA"
            }]
        }, {
            "product": {
                "id": "1884053000012868763"
            }, 
            "list_price": 1.67,
            "quantity": 1,
            "Discount": 0,
            "Discount_Amount": 0,
            "line_tax": [{
                "percentage": 20,
                "name": "TVA"
            }]
        }],
        "Adjustment": 0,
        "Montant_TPH": 0,
        "TVA_sur_DEEE_TPH": "0",
        "Taxes_DEEE_TPH": "TPH",
        "Frais_de_port_HT": "2.000000",
        "Frais_de_port_TTC": "2.000000",
        "Nb_produits": 3,
        "Quantite_totale": 3
    }],
    "duplicate_check_fields": ["ID_Lip"],
    "trigger": []
}

但是当我使用json_encode()函数时,它不会向我返回对象json而是一个空数组,并且在代码中也看不到任何语法错误。我不知道这是否真的是语法错误或其他错误。有人可以帮助我知道问题出在哪里/在哪里? 预先谢谢你!

2 个答案:

答案 0 :(得分:0)

“ Product_Name”附近的json语法有误。

这些值必须是字符串,因此必须在“”之间。

如果您遇到解决问题,请告诉我们:)

祝你有美好的一天!

答案 1 :(得分:-1)

解决方案

改为使用json_decode($jsondata, true)

详细说明:json_decode

要使用的JSON

您的json-string中也有错误

这是要使用的合适的json:

{
    "data": [{
        "Contact_Name": {
            "id": "1884053000034678068"
        },
        "Subject": "1 - XKBKNABJK",
        "Status": "Sans Impact Stock",
        "Entity": "",
        "Etat": "CM - Annul\u00e9",
        "Invoice_Date": "2017-11-21",
        "Discount": 0.000000,
        "Tax": ["TVA"],
        "Total_HT": "55.000000",
        "Total_TVA": 0,
        "Methode_de_paiement": "Payment by check",
        "ID_Lip": "1",
        "Origine": "CM",
        "Type_de_Facture": "Solde",
        "Transporteur": "My carrier",
        "Reference_Colis": "",
        "Billing_Street": "16, Main street",
        "Compl_ment_Facturation": "2nd floor",
        "Billing_City": "Miami",
        "Billing_Country": "\u00c9tats-Unis",
        "Billing_Code": "33133",
        "Shipping_Street": "16, Main street",
        "Compl_ment_Livraison": "2nd floor",
        "Shipping_City": "Miami",
        "Shipping_Country": "\u00c9tats-Unis",
        "Shipping_Code": "33133",
        "Product_Details": [{
            "product": {
                "id": "1884053000030235273"
            },
            "Product_Name": "Blouse - Color: White, Size: M",
            "list_price": 22.5,
            "quantity": 1,
            "Discount": 0,
            "Discount_Amount": 0,
                "line_tax": [{
                "percentage": 20,
                "name": "TVA"
            }]
        }, {
            "product": {
                "id": "1884053000030235273"
            },
            "Product_Name": "Printed Dress - Color: Orange, Size: S",
            "list_price": 21.67,
            "quantity": 1,
            "Discount": 0,
            "Discount_Amount": 0,
            "line_tax": [{
                "percentage": 20,
                "name": "TVA"
            }]
        }, {
            "product": {
                "id": "1884053000012868763"
            }, 
            "list_price": 1.67,
            "quantity": 1,
            "Discount": 0,
            "Discount_Amount": 0,
            "line_tax": [{
                "percentage": 20,
                "name": "TVA"
            }]
        }],
        "Adjustment": 0,
        "Montant_TPH": 0,
        "TVA_sur_DEEE_TPH": "0",
        "Taxes_DEEE_TPH": "TPH",
        "Frais_de_port_HT": "2.000000",
        "Frais_de_port_TTC": "2.000000",
        "Nb_produits": 3,
        "Quantite_totale": 3
    }],
    "duplicate_check_fields": ["ID_Lip"],
    "trigger": []
}

为了未来

检查您的json here以获得安全的结果。

我希望这对您有帮助