从java中的嵌套JSON中检索值

时间:2015-03-26 18:52:17

标签: java json

我有一个JSON,如下所述。

 {"Orders"
   [{"BusinessUnit":"TS",
     "DeliveryDetails":
             [{"SlotStartDateTime":"2015-03-30T16","DeliveryOption":"Home    
             Delivery","ReservationID":"13349259","PersonInfoShipTo":
             {"Address":"OrganizationName":"HP","BuildingTypeID":"",
             "IsCommercialAddress":"false","PostalCode":"56001",
             "City":"Bangalore","AddressLine3":"A3","AddressLine2":"A2",
             "AddressLine1":"A1","IsPAFValidated":"true",
             "GridRef":"0473601734","State":"KA","AddressLine4":"A4",
             "Country":"IN"}},"ShipNode":"NODEUK","SlotEndDateTime":"2015-
              03-30T17"}],
    "FulfilmentID":"a9466f83-938d-4115-a3d4-62ff4bdcd1b6",
    "OrderTypeIndicator":"SalesOrder"}]}

问题: 我可以采用BusinessUnitFulfilmentID&的值OrderTypeIndicator。但是,我无法获取内部json对象DeliveryDetails中存在的值。任何人都可以帮助我取得这些价值

1 个答案:

答案 0 :(得分:1)

“DeliveryDetails”被映射到一个json数组对象,里面有一个json对象。

尝试做:

json.getJSONArray("Orders").get(0).get("DeliveryDetails").get(0)

当然最好首先检查密钥是否存在以及在检索实际索引之前返回的json数组的大小。