2011-11-12 48 views
0

我想創建一個JSON結構如下:要創建JsonArray

{ 
    "firstName": "John", 
    "lastName": "Smith", 
    "address": { 
     "streetAddress": "21 2nd Street", 
     "city": "New York", 
     "state": "NY", 
     "postalCode": 10021 
    }, 
    "phoneNumbers": [ 
     "212 732-1234", 
     "646 123-4567" 
    ] 
} 

使用添加屬性的方法,我能夠創造firstName和lastName,但還沒有想出如何在Java中創建地址還有聯繫電話。 請幫我解決這個問題; Ajay

回答

1

你需要做的是創建一個名爲Address的新類,它具有這些屬性(字符串streetAdress,city,state,postalcode)。結構本身就會擁有該類的一個實例。

此外,我不會讓郵政編碼爲int。將其保留爲字符串。

PhoneNumbers將是一個字符串列表(除非你想用它做點什麼)。