如何通过Android应用程序中的GSon解析器解析枚举值

时间:2012-02-10 10:28:24

标签: android json

我收到回复

{
    "returnCode": "0",
    "message": "Sucessfully get  credit card for value(1) ",
    "token": "",
    "CreditCard": {
        "class": "CreditCard",
        "id": 1,
        "bankName": "NA",
        "cardNumber": "1233435467789",
        "ccvNumber": "3455",
        "dateCreated": "2012-02-10T10:20:06Z",
        "expiryDate": "2012-02-29T18:30:00Z",
        "expiryDateStr": null,
        "lastUpdated": "2012-02-10T10:20:06Z",
        "securityCode": null,
        "type": {
            "enumType": "CreditCardType",
            "name": "Visa"
        },
        "user": {
            "class": "User",
            "id": 4
        }
    }
}

我无法更改服务器代码,因此我该如何解析它。任何帮助..

1 个答案:

答案 0 :(得分:0)

你的枚举:

enum CreditCardType{
  Visa, MasterCard, Diners
}

在解析时,到达type

//param is JSONObject
CreditCardType card = CreditCardType.valueOf(param.getString("name"));