1

从服务的响应中,有时我得到JSONArray或有时JSONObject。那么,我如何识别它是 JsonObject 还是 JsonArray。

这是我对两个方向的回应,

它是一个 JsonArray,如果没有数据,则给出空白 json 数组

[]

它是一个 JsonObject,

{
"12": {
    "order_number": "12",
    "revision_id": "19",
    "revision_uid": "1",
    "mail": "",
    "status": "pending",
    "log": "",
    "revision_timestamp": "1372922775",
    "revision_hostname": "192.168.1.104",
    "data": [],
    "order_id": "12",
    "type": "commerce_order",
    "uid": "0",
    "created": "1372922775",
    "changed": "1372922775",
    "hostname": "",
    "commerce_line_items": [
        "12"
    ],
    "commerce_order_total": {
        "amount": "24000",
        "currency_code": "USD",
        "data": {
            "components": [
                {
                    "name": "base_price",
                    "price": {
                        "amount": 24000,
                        "currency_code": "USD",
                        "data": []
                    },
                    "included": true
                }
            ]
        }
    },
    "commerce_customer_billing": "15",
    "rdf_mapping": [],
    "commerce_order_total_formatted": "$240.00",
    "commerce_line_items_entities": {
        "12": {
            "line_item_id": "12",
            "order_id": "12",
            "type": "product",
            "line_item_label": "Rebok 435",
            "quantity": "2.00",
            "created": "1372922753",
            "changed": "1372922775",
            "data": [],
            "commerce_unit_price": {
                "amount": "12000",
                "currency_code": "USD",
                "data": {
                    "components": [
                        {
                            "name": "base_price",
                            "price": {
                                "amount": "12000",
                                "currency_code": "USD",
                                "data": {
                                    "components": []
                                }
                            },
                            "included": true
                        }
                    ]
                }
            },
            "commerce_total": {
                "amount": "24000",
                "currency_code": "USD",
                "data": {
                    "components": [
                        {
                            "name": "base_price",
                            "price": {
                                "amount": 24000,
                                "currency_code": "USD",
                                "data": {
                                    "components": []
                                }
                            },
                            "included": true
                        }
                    ]
                }
            },
            "commerce_product": "2",
            "commerce_display_path": "",
            "rdf_mapping": [],
            "line_item_title": "Rebok 435",
            "commerce_unit_price_formatted": "$120.00",
            "commerce_total_formatted": "$240.00"
        }
    },
    "commerce_customer_billing_entities": {
        "15": {
            "revision_id": "15",
            "revision_uid": "1",
            "status": "1",
            "log": "",
            "revision_timestamp": "1372922783",
            "data": false,
            "profile_id": "15",
            "type": "billing",
            "uid": "0",
            "created": "1372922737",
            "changed": "1372922783",
            "commerce_customer_address": {
                "country": "IN",
                "administrative_area": "",
                "sub_administrative_area": null,
                "locality": "",
                "dependent_locality": null,
                "postal_code": "",
                "thoroughfare": "",
                "premise": "",
                "sub_premise": null,
                "organisation_name": null,
                "name_line": "",
                "first_name": null,
                "last_name": null,
                "data": null
            },
            "rdf_mapping": []
        }
    }
},
"13": {
    "order_number": "13",
    "revision_id": "20",
    "revision_uid": "1",
    "mail": "",
    "status": "pending",
    "log": "",
    "revision_timestamp": "1372922812",
    "revision_hostname": "192.168.1.104",
    "data": [],
    "order_id": "13",
    "type": "commerce_order",
    "uid": "0",
    "created": "1372922812",
    "changed": "1372922812",
    "hostname": "",
    "commerce_line_items": [
        "13"
    ],
    "commerce_order_total": {
        "amount": "1150000",
        "currency_code": "USD",
        "data": {
            "components": [
                {
                    "name": "base_price",
                    "price": {
                        "amount": 1150000,
                        "currency_code": "USD",
                        "data": []
                    },
                    "included": true
                }
            ]
        }
    },
    "commerce_customer_billing": "16",
    "rdf_mapping": [],
    "commerce_order_total_formatted": "$11,500.00",
    "commerce_line_items_entities": {
        "13": {
            "line_item_id": "13",
            "order_id": "13",
            "type": "product",
            "line_item_label": "mens_sports_shose",
            "quantity": "5.00",
            "created": "1372922806",
            "changed": "1372922812",
            "data": [],
            "commerce_unit_price": {
                "amount": "230000",
                "currency_code": "USD",
                "data": {
                    "components": [
                        {
                            "name": "base_price",
                            "price": {
                                "amount": "230000",
                                "currency_code": "USD",
                                "data": {
                                    "components": []
                                }
                            },
                            "included": true
                        }
                    ]
                }
            },
            "commerce_total": {
                "amount": "1150000",
                "currency_code": "USD",
                "data": {
                    "components": [
                        {
                            "name": "base_price",
                            "price": {
                                "amount": 1150000,
                                "currency_code": "USD",
                                "data": {
                                    "components": []
                                }
                            },
                            "included": true
                        }
                    ]
                }
            },
            "commerce_product": "1",
            "commerce_display_path": "",
            "rdf_mapping": [],
            "line_item_title": "Reboke",
            "commerce_unit_price_formatted": "$2,300.00",
            "commerce_total_formatted": "$11,500.00"
        }
    },
    "commerce_customer_billing_entities": {
        "16": {
            "revision_id": "16",
            "revision_uid": "1",
            "status": "1",
            "log": "",
            "revision_timestamp": "1372922812",
            "data": false,
            "profile_id": "16",
            "type": "billing",
            "uid": "0",
            "created": "1372922797",
            "changed": "1372922812",
            "commerce_customer_address": {
                "country": "IN",
                "administrative_area": "",
                "sub_administrative_area": null,
                "locality": "",
                "dependent_locality": null,
                "postal_code": "",
                "thoroughfare": "",
                "premise": "",
                "sub_premise": null,
                "organisation_name": null,
                "name_line": "",
                "first_name": null,
                "last_name": null,
                "data": null
            },
            "rdf_mapping": []
        }
    }
}
}

请告诉我如何识别响应。是 JSONObject 还是 JSONArray ?

4

0 回答 0