Rahul February 2016

I am using retrofit2 for networking calls and GSON as a converter to parse JSON. I want to parse a json structure in which keys are not fixed

I am using retrofit2 for networking calls and GSON as a converter to parse JSON. I want to parse a json structure in which keys are not fixed. Need help in creating models/beans for the same. Here's the JSON I am trying to parse.The keys inside the user_card array keep on changing.

{
"isNewVersionAvailable": false
"ishotelslive": true
"error": false
"message": "Card information found."
"cardArray": {
"status": 1
"msg": "Cards fetched Succesfully"
"user_cards": {
"cxb39d6c8bf1b4d5bb6e479fbaca931bb14880e8511": {
"name_on_card": "asc"
"card_name": "2346"
"expiry_year": "2016"
"expiry_month": "06"
"card_type": "CC"
"card_token": "cxb39d6c8bf1b4d5bb6e479fbaca931bb14880e8511"
"is_expired": 0
"card_mode": "CC"
"card_cvv": 0
"card_no": "512345XXXXXX2346"
"card_brand": "MASTERCARD"
"card_bin": "512345"
"isDomestic": "Y"
}
"re6e69c2482160d8b2190b5dc36af2d46472ba5aafa": {
"name_on_card": "asc"
"card_name": "1111"
"expiry_year": "2016"
"expiry_month": "06"
"card_type": "CC"
"card_token": "re6e69c2482160d8b2190b5dc36af2d46472ba5aafa"
"is_expired": 0
"card_mode": "CC"
"card_cvv": 0
"card_no": "411111XXXXXX1111"
"card_brand": "VISA"
"card_bin": "411111"
"isDomestic": "Y"
}
"tye6977832bc0134be29317af77a6892b3e916dd69e": {
"name_on_card": "Shoban Test"
"card_name": "Shoban Test"
"expiry_year": "2017"
"expiry_month": "05"
"card_type": "CC"
"card_token": "tye6977832bc0134be29317af77a6892b3e916dd69e"
"is_expired": 0
"card_mode": "CC"
"card_cvv": 0
"card_no": "512345XXXXXX2346"
"card_brand": "MASTERCARD"
"card_bin": "512345"
"isDomestic": "Y"
}
"gh57f9e8ee6628e5c1cf1228d8491a08f966cae9f7s": {
"name_on_card": "asc"
"card_name": "9984"
"expiry_year": "2016"
"expiry_month": "06"
"card_type": "VISA"
"card_token": "gh57f9e8ee6628e5c1cf1228d8491a08f966cae9f7s"
"is_expired": 0
"card_mode": "DC"
"card_cvv": 0
"card_no": "434679XXXXXX9984"
"card_brand": "VISA"
"card_bin": "434679"
"isDomestic": "Y"
}
}
}
}

Answers


Mathieu Hausherr February 2016

Just use a Map field in your CardArray object

import java.util.Map;

class Parent {
    public boolean isNewVersionAvailable;
    public boolean ishotelslive;
    public boolean error;
    public String message;
    public CardArray cardArray;
}

class CardArray {
    public int status;
    public String msg;
    public Map<String, Card> user_cards;
}

class Card {
    public String name_on_card;
    public String card_name;
    public String expiry_year;
    public String expiry_month;
    public String card_type;
    public String card_token;
    public int is_expired;
    public String card_mode;
    public String card_cvv;
    public String card_no;
    public String card_brand;
    public String card_bin;
    public String isDomestic;
}

Post Status

Asked in February 2016
Viewed 3,711 times
Voted 13
Answered 1 times

Search




Leave an answer