I have got the JSON as below
{
"brands": [
{
"name": "ACC",
"quantity": "0",
"listedbrandID": 1,
"status": "0"
}
],
"others": [
{
"name": "dd",
"quantity": "55",
"listedbrandID": 1
},
{
"name": "dd",
"quantity": "55",
"listedbrandID": 1
}
]
}
I am trying to remove the dupcate JSON objects from the Others array
I was trying it this way
String JSON = "{
"brands": [
{
"name": "ACC",
"quantity": "0",
"listedbrandID": 1,
"status": "0"
}
],
"others": [
{
"name": "dd",
"quantity": "55",
"listedbrandID": 1
},
{
"name": "dd",
"quantity": "55",
"listedbrandID": 1
}
]
}" ;
JSONObject json_obj = new JSONObject(json);
JSONArray array = json_obj.getJSONArray("others");
HashSet<Others> map = new HashSet<Others>();
for (int k = 0; k < array.length(); k++) {
Others otherbrands = new Others();
String name = array.getJSONObject(k).getString("name");
String quantity = array.getJSONObject(k).getString("quantity");
String listedbrandID = array.getJSONObject(k).getString("listedbrandID");
if(name!=null && !name.trim().equals(""))
{
otherbrands.setName(name);
otherbrands.setQuantity(quantity);
otherbrands.setListedbrandID(listedbrandID);
map.add(otherbrands);
}
}
for (int k = 0; k < array.length(); k++)
{
String name = array.getJSONObject(k).getString("name");
System.out.println(name);
if(!map.contains(name.trim()))
{
array.remove(k);
}
}
System.out.println(json_obj);
}
}
import java.util.Arrays;
public class Others {
private String name ;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getQuantity() {
return quantity;
}
public void setQuantity(String quantity) {
this.quantity = quantity;
}
public String getListedbrandID() {
return listedbrandID;
}
public void setListedbrandID(String listedbrandID) {
this.listedbrandID = listedbrandID;
}
private String quantity ;
private String listedbrandID ;
#Override
public boolean equals (Object other)
{
if (!(other instanceof Others))
return false;
Others ob = (Others) other;
return name.equals(ob.name) &&
quantity.equals(ob.quantity);
}
#Override
public int hashCode ()
{
return Arrays.hashCode(new String[]{quantity,name});
}
}
But its printing the entire JSON
There are some issues with your code:
System.out.println(json_obj); is printing your initial JSON object, you do not seem to be iterating over the set and printing its content. Try:
for(Other other : map) System.out.println("Name : " + other.getName() + " ...");
I think you need to add listedbrandID in your equals method.
To get JSON with only the unique items, you could simply emit the resultant HashMap. Please refer to this previous SO question for more information.
Related
I am trying to parse a JSON with Java and I do not get the expected result.
Example json:
"productId": "NDAtOS0wLS0=",
"branchId": 5,
"branchCustomers":[
{"branchId":615,"customerNumber":4918,"products":[]},
{"branchId":615,"customerNumber":9753,"products":[]},
{"branchId":615,"customerNumber":9761,"products":[]}
],
"customerNumber": 240,
"subAccountNumber": 0,
"productType": 9,
"openingDate": "2016-10-02",
"values": [
{
"key": "accountingMethod",
"value": "1"
},
{
"key": "accountType",
"value": "1"
},
{
"key": "assetCashAccountId-branchId",
"value": "615"
},
{
"key": "assetCashAccountId-customerNumber",
"value": "4041240"
},
{
"key": "assetCashAccountId-subAccountNumber",
"value": "2"
},
{
"key": "assetMarginAccountId-branchId",
"value": "615"
},
{
"key": "assetMarginAccountId-customerNumber",
"value": "4041240"
},
{
"key": "assetMarginAccountId-subAccountNumber",
"value": "2"
},
{
"key": "blockingAmount",
"value": "1000000"
},
{
"key": "collateral",
"value": "C"
},
{
"key": "collateralBlockingType",
"value": "1"
},
{
"key": "executingSecurityAccountId-branchId",
"value": "615"
},
{
"key": "executingSecurityAccountId-customerNumber",
"value": "4041240"
},
{
"key": "executingSecurityAccountId-subAccountNumber",
"value": "0"
},
{
"key": "limit",
"value": "1000000"
},
{
"key": "marginAccountId-branchId",
"value": "0"
},
{
"key": "marginAccountId-customerNumber",
"value": "0"
},
{
"key": "marginAccountId-subAccountNumber",
"value": "0"
},
{
"key": "marginMarkup",
"value": "100"
},
{
"key": "rolfNolanLedger",
"value": "B01429"
},
{
"key": "settlementMethod",
"value": "1"
}
]
}
]
}],
"instances": []
}
Not all the JSONs have this structure, some may miss some of the fields. I created some DTO classes for parsing it. This is my code:
public class Response {
private String partnerId;
private byte branchId;
private long customerNumber;
private Long subAccountNumber;
private Byte productType;
private String openingDate;
private String closingDate;
private List<Values> values;
private List<Instances> instances;
private List<BranchCustomers> branchCustomers;
public String getProductid() {
return partnerId;
}
public void setProductid(String productid) {
this.partnerId = productid;
}
public byte getBranchid() {
return branchId;
}
public void setBranchid(byte branchid) {
this.branchId = branchid;
}
public long getCustomernumber() {
return customerNumber;
}
public void setCustomernumber(long customernumber) {
this.customerNumber = customernumber;
}
public Long getSubaccountnumber() {
return subAccountNumber;
}
public void setSubaccountnumber(Long subAccountNumber) {
this.subAccountNumber = subAccountNumber;
}
public Byte getProducttype() {
return productType;
}
public void setProducttype(Byte productType) {
this.productType = productType;
}
public String getOpeningdate() {
return openingDate;
}
public void setOpeningdate(String openingDate) {
this.openingDate = openingDate;
}
public String getClosingdate() {
return closingDate;
}
public void setClosingdate(String closingDate) {
this.closingDate = closingDate;
}
public List<Values> getValues() {
return values;
}
public void setValues(List<Values> values) {
this.values = values;
}
public List<Instances> getInstances() {
return instances;
}
public void setInstances(List<Instances> instances) {
this.instances = instances;
}
public List<BranchCustomers> getBranchCustomers() {
return branchCustomers;
}
public void setBranchCustomers(List<BranchCustomers> branchCustomers) {
this.branchCustomers = branchCustomers;
}
}
public class BranchCustomers {
private byte branchId;
private long customerNumber;
private List<Products> products;
public byte getBranchid() {
return branchId;
}
public void setBranchid(byte branchId) {
this.branchId = branchId;
}
public long getCustomernumber() {
return customerNumber;
}
public void setCustomernumber(long customerNumber) {
this.customerNumber = customerNumber;
}
public List<Products> getProducts() {
return products;
}
public void setProducts(List<Products> products) {
this.products = products;
}
}
public void parseJson(String response) {
try{
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Response result = mapper.readValue(response, Response.class);
System.out.println(result);
} catch (JsonMappingException e) {
e.printStackTrace
} catch (JsonProcessingException e) {
e.printStackTrace();
}
The other DTO classes "Values", "Instances" and "Products" are empty at this moment, but I do not need them yet. The problem is that the "result" value has the right structure and the right fields, but they are null or 0, depending on their data type and I cannot see what I have done wrong.
Can anyone help me?
Your getters, setters have incorrect names.
As an example, in your Response object, you have getProductid, getBranchid, and getCustomernumber with similar setter methods. Your JSON however has productId, branchId, and customerNumber. They differ as you didn't use the correct capitalization. You should use camelCase, which would turn your naming to getProductId, getBranchId, and getCustomerNumber. This then matches the JSON and it will map accordingly.
This:
public class Response {
private String partnerId;
private byte branchId;
private long customerNumber;
private Long subAccountNumber;
private Byte productType;
private String openingDate;
private String closingDate;
private List<BranchCustomers> branchCustomers;
public String getProductId() {
return partnerId;
}
public void setProductId(String productid) {
this.partnerId = productid;
}
public byte getBranchId() {
return branchId;
}
public void setBranchId(byte branchid) {
this.branchId = branchid;
}
public long getCustomerNumber() {
return customerNumber;
}
public void setCustomerNumber(long customernumber) {
this.customerNumber = customernumber;
}
...
Leads to:
Response{productId='NDAtOS0wLS0=', branchId=5, customerNumber=240 ...
As a sidenote, I suggest either auto-generating the boilerplate code with your IDE or look into Project Lombok which can generate the boilerplate code by just adding an annotation. This also stops you from making naming mistakes.
Also, try and get a toString method for each pojo, as it will help you during logging or debugging.
How can I describe the POJO for such an answer so that the retrofit understands it? the response comes in this form. I cut it, in order to see the structure of JSON. nested objects I converted to POJO. I want to learn how to convert the main object?
[
[
"all_areas",
{
"6": {
"id": "6",
"parent_id": "0",
"left_key": "1",
"right_key": "6594",
"level": "1",
"title": "Вся Россия",
"alias": "vsya_rossiya",
"sort": "1",
"navigatorListItems": []
},
"7": {
"id": "7",
"parent_id": "6",
"left_key": "2",
"right_key": "31",
"level": "2",
"title": "Адыгея респ.",
"alias": "adygeya_resp",
"sort": "1",
"navigatorListItems": []
}
}
],
[
"current_rubrics",
[
{
"id": "7",
"parent_id": "6",
"left_key": "2",
"right_key": "19",
"level": "2",
"title": "Недвижимость",
"alias": "nedvizhimost",
"sort": "1"
},
{
"id": "8",
"parent_id": "6",
"left_key": "20",
"right_key": "47",
"level": "2",
"title": "Транспорт",
"alias": "transport",
"sort": "2"
}
]
]
]
I suppose this is what a Json should look like
{
"all_areas": [{
"6": {
"id": "6",
"parent_id": "0",
"left_key": "1",
"right_key": "6594",
"level": "1",
"title": "Вся Россия",
"alias": "vsya_rossiya",
"sort": "1",
"navigatorListItems": []
}
},
{
"7": {
"id": "7",
"parent_id": "6",
"left_key": "2",
"right_key": "31",
"level": "2",
"title": "Адыгея респ.",
"alias": "adygeya_resp",
"sort": "1",
"navigatorListItems": []
}
}
],
"current_rubrics": [{
"id": "7",
"parent_id": "6",
"left_key": "2",
"right_key": "19",
"level": "2",
"title": "Недвижимость",
"alias": "nedvizhimost",
"sort": "1"
},
{
"id": "8",
"parent_id": "6",
"left_key": "20",
"right_key": "47",
"level": "2",
"title": "Транспорт",
"alias": "transport",
"sort": "2"
}
]
}
Copy this and convert it to pojo using the website http://www.jsonschema2pojo.org or any another website which does the conversion for you
This will be your main model class:
public class Testing
{
private 7 7;
private 6 6;
public 7 get7 ()
{
return 7;
}
public void set7 (7 7)
{
this.7 = 7;
}
public 6 get6 ()
{
return 6;
}
public void set6 (6 6)
{
this.6 = 6;
}
#Override
public String toString()
{
return "ClassPojo [7 = "+7+", 6 = "+6+"]";
}
}
then there will be two subclasses (7.java & 6.java):
7.java
public class 7
{
private String[] navigatorListItems;
private String id;
private String left_key;
private String title;
private String sort;
private String level;
private String alias;
private String right_key;
private String parent_id;
public String[] getNavigatorListItems ()
{
return navigatorListItems;
}
public void setNavigatorListItems (String[] navigatorListItems)
{
this.navigatorListItems = navigatorListItems;
}
public String getId ()
{
return id;
}
public void setId (String id)
{
this.id = id;
}
public String getLeft_key ()
{
return left_key;
}
public void setLeft_key (String left_key)
{
this.left_key = left_key;
}
public String getTitle ()
{
return title;
}
public void setTitle (String title)
{
this.title = title;
}
public String getSort ()
{
return sort;
}
public void setSort (String sort)
{
this.sort = sort;
}
public String getLevel ()
{
return level;
}
public void setLevel (String level)
{
this.level = level;
}
public String getAlias ()
{
return alias;
}
public void setAlias (String alias)
{
this.alias = alias;
}
public String getRight_key ()
{
return right_key;
}
public void setRight_key (String right_key)
{
this.right_key = right_key;
}
public String getParent_id ()
{
return parent_id;
}
public void setParent_id (String parent_id)
{
this.parent_id = parent_id;
}
#Override
public String toString()
{
return "ClassPojo [navigatorListItems = "+navigatorListItems+", id = "+id+", left_key = "+left_key+", title = "+title+", sort = "+sort+", level = "+level+", alias = "+alias+", right_key = "+right_key+", parent_id = "+parent_id+"]";
}
}
6.java
public class 6
{
private String[] navigatorListItems;
private String id;
private String left_key;
private String title;
private String sort;
private String level;
private String alias;
private String right_key;
private String parent_id;
public String[] getNavigatorListItems ()
{
return navigatorListItems;
}
public void setNavigatorListItems (String[] navigatorListItems)
{
this.navigatorListItems = navigatorListItems;
}
public String getId ()
{
return id;
}
public void setId (String id)
{
this.id = id;
}
public String getLeft_key ()
{
return left_key;
}
public void setLeft_key (String left_key)
{
this.left_key = left_key;
}
public String getTitle ()
{
return title;
}
public void setTitle (String title)
{
this.title = title;
}
public String getSort ()
{
return sort;
}
public void setSort (String sort)
{
this.sort = sort;
}
public String getLevel ()
{
return level;
}
public void setLevel (String level)
{
this.level = level;
}
public String getAlias ()
{
return alias;
}
public void setAlias (String alias)
{
this.alias = alias;
}
public String getRight_key ()
{
return right_key;
}
public void setRight_key (String right_key)
{
this.right_key = right_key;
}
public String getParent_id ()
{
return parent_id;
}
public void setParent_id (String parent_id)
{
this.parent_id = parent_id;
}
#Override
public String toString()
{
return "ClassPojo [navigatorListItems = "+navigatorListItems+", id = "+id+", left_key = "+left_key+", title = "+title+", sort = "+sort+", level = "+level+", alias = "+alias+", right_key = "+right_key+", parent_id = "+parent_id+"]";
}
}
I have a basic spring boot standalone executable jar using the bott 2.0.0.0 I think this is simple, but Google won't give up the answer. :) I am am using the latest stable jackson versions (2.9.4) but they ARE being managed by spring. This is a Boolean problem:
here is the JSON I am trying to turn into a Java Pojo (it is wrapped in a higher object but I don't think that's the problem. I haveing problems qith the boolean.
{
"guid": "a5182918-8d69-11e6-acb6-0a97227b08ed",
"organizationId": 1,
"region": "Tariff Picker",
"stages": [{
"nextStages": [],
"activities": [{
"nextActivities": [],
"name": "New Activity",
"suspensionReason": "",
"rules": [],
"isSuspend": false,
"sequence": 1,
"allowedRoles": [{
"userApplications": [],
"name": "submitApplication",
"organizationId": 0,
"workQueues": [],
"roleApplicationsForSystemRoleId": [],
"isPublic": 0,
"widgetRoles": [],
"userRoles": [],
"roleTariffReports": [],
"roleTypeId": 0,
"distributionListRoles": [],
"organizationRoles": [],
"publicationRoles": [],
"roleTariffDataSets": [],
"roleApplicationsForApplicationRoleId": [],
"workQueueArchives": [],
"id": 11,
"rolePrivileges": []
}],
"label": "New Activity",
"irrevocable": false,
"stageId": 0,
"id": 0,
"buttonPrompt": "Submit",
"guid": "2e195e0c-83d2-491f-b2e8-3ad1159d1d99",
"dataBlock": {
"sections": [{
"info": "",
"prompt": "",
"name": "First Section",
"sequence": 0,
"fields": [],
"gatingConditions": [],
"guid": "480d160c-c34f-4022-97b0-e8a1f28c49ae",
"id": -2
}],
"prompt": "",
"id": -1,
"name": ""
},
"autoExecute": false
}],
"name": "Tariff Selection Stage",
"sequence": 1,
"rules": [],
"completionMessage": "",
"guid": "65a73280-c587-486f-be8b-9107426f4730",
"id": 0,
"description": ""
}],
"stop": "3000-01-01",
"workflowTypeId": 2,
"isUserAction": false,
"start": "1900-01-01",
"isSandbox": false,
"gatingConditions": [],
"tariffId": 49,
"businessCalendarId": 1,
"applicationForms": [],
"id": 49,
"rules": []
}
I am getting an error saying there is not a field named "isSuspend" and as you can see it is there (line 3) and even set to false. here is my pojo:
private int id;
private String name;
private List<DataBlockObject> dataBlocks;
private int sequence;
private List<RuleObject> rules;
private List<AllowedRoleObject> allowedRoles;
private List<NextActivityObject> nextActivities;
private List<ActivityPermissionObject> activityPermissions;
private boolean autoExecute;
private boolean irrevocable;
private String label;
private String buttonPrompt;
private int stageId;
private boolean isSuspend;
private String suspensionReason;
private String guid;
private List<ActivitySubmitErrorPromptObject> activitySubmitErrorPrompts;
private int activitySubmitErrorTimeout;
private String breadcrumbClass;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<DataBlockObject> getDataBlocks() {
return dataBlocks;
}
public void setDataBlocks(List<DataBlockObject> dataBlocks) {
this.dataBlocks = dataBlocks;
}
public int getSequence() {
return sequence;
}
public void setSequence(int sequence) {
this.sequence = sequence;
}
public List<RuleObject> getRules() {
return rules;
}
public void setRules(List<RuleObject> rules) {
this.rules = rules;
}
public List<AllowedRoleObject> getAllowedRoles() {
return allowedRoles;
}
public void setAllowedRoles(List<AllowedRoleObject> allowedRoles) {
this.allowedRoles = allowedRoles;
}
public List<NextActivityObject> getNextActivities() {
return nextActivities;
}
public void setNextActivities(List<NextActivityObject> nextActivities) {
this.nextActivities = nextActivities;
}
public List<ActivityPermissionObject> getActivityPermissions() {
return activityPermissions;
}
public void setActivityPermissions(List<ActivityPermissionObject> activityPermissions) {
this.activityPermissions = activityPermissions;
}
public boolean isAutoExecute() {
return autoExecute;
}
public void setAutoExecute(boolean autoExecute) {
this.autoExecute = autoExecute;
}
public boolean isIrrevocable() {
return irrevocable;
}
public void setIrrevocable(boolean irrevocable) {
this.irrevocable = irrevocable;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public String getButtonPrompt() {
return buttonPrompt;
}
public void setButtonPrompt(String buttonPrompt) {
this.buttonPrompt = buttonPrompt;
}
public int getStageId() {
return stageId;
}
public void setStageId(int stageId) {
this.stageId = stageId;
}
public boolean isSuspend() {
return isSuspend;
}
public void setSuspend(boolean isSuspend) {
this.isSuspend = isSuspend;
}
public String getSuspensionReason() {
return suspensionReason;
}
public void setSuspensionReason(String suspensionReason) {
this.suspensionReason = suspensionReason;
}
public String getGuid() {
return guid;
}
public void setGuid(String guid) {
this.guid = guid;
}
public List<ActivitySubmitErrorPromptObject> getActivitySubmitErrorPrompts() {
return activitySubmitErrorPrompts;
}
public void setActivitySubmitErrorPrompts(List<ActivitySubmitErrorPromptObject> activitySubmitErrorPrompts) {
this.activitySubmitErrorPrompts = activitySubmitErrorPrompts;
}
public int getActivitySubmitErrorTimeout() {
return activitySubmitErrorTimeout;
}
public void setActivitySubmitErrorTimeout(int activitySubmitErrorTimeout) {
this.activitySubmitErrorTimeout = activitySubmitErrorTimeout;
}
public String getBreadcrumbClass() {
return breadcrumbClass;
}
public void setBreadcrumbClass(String breadcrumbClass) {
this.breadcrumbClass = breadcrumbClass;
}
Can the problem be because of the getter and setter names for the field isSuspend? Try naming getter and setter getIsSuspend and setIsSuspend
Your POJO doesn't follow Java Beans naming convention. If you want Jackson to not look at the getters/setters, but only fields, see this: how to specify jackson to only use fields - preferably globally
I have an unsorted array of players that came through a JSON response:
"participants": [{
"participant": {
"rank": 3,
"name": "I Tied for third",
}
}, {
"participant": {
"rank": 1,
"name": "I got first",
}
}, {
"participant": {
"rank": 3,
"name": "Also tied for third",
}
}, {
"participant": {
"rank": 2,
"name": "I got second",
}
}]
I would like to sort this somehow so that I can eventually print out both the player's name, and their rank... but in order.
I thought about possibly putting the results of the array into a TreeMap, but then I realised that TreeMaps require unique array keys and that wouldn't work:
Map<Integer, String> players = new TreeMap<>();
for (int i = 0; i < participants.length(); i++) {
JSONObject object = participants.getJSONObject(i);
JSONObject participant = object.getJSONObject("participant");
players.put(participant.getInt("rank"), participant.getString("name"));
}
So is there another way to get this done?
With #VivekMishra helping, I was able to get it working:
ArrayList<PlayerObject> players = new ArrayList<>();
for (int i = 0; i < participants.length(); i++) {
JSONObject object = participants.getJSONObject(i);
JSONObject participant = object.getJSONObject("participant");
players.add(new PlayerObject(participant.getInt("rank"), participant.getString("name")));
}
Collections.sort(players);
With:
class PlayerObject implements Comparable<PlayerObject> {
private int rank;
private String name;
public PlayerObject(int r, String n) {
setRank(r);
setName(n);
}
public int compareTo(PlayerObject other) { return rank - other.rank; }
public int getRank() { return rank; }
public void setRank(int text) { rank = text; }
public String getName() { return name; }
public void setName(String text) { name = text; }
}
I have not been able to map this nested array rows->elements to my javabean. Is gson actually capable of handling this kind of mapping? I also tried a different approach, which you can see if you look at the commented out Java code.
package scratch;
import java.util.ArrayList;
import java.util.List;
/*
{
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "897 mi",
"value" : 1443464
},
"duration" : {
"text" : "14 hours 32 mins",
"value" : 52327
},
"status" : "OK"
}
]
},
{
"elements" : [
{
"distance" : {
"text" : "378 mi",
"value" : 607670
},
"duration" : {
"text" : "6 hours 22 mins",
"value" : 22908
},
"status" : "OK"
}
]
}
]
}
*/
public class GeoZipCodesBean2 {
// private Elem[][] rows;
// public Elem[][] getRows() {
// return rows;
// }
//
// public void setRows(Elem[][] rows) {
// this.rows = rows;
// }
private List<List<Elem>>rows;
public List<List<Elem>> getRows() {
return rows;
}
public void setRows(List<List<Elem>> rows) {
this.rows = rows;
}
public static class Elem {
private Distance distance;
private Duration duration;
public Distance getDistance() {
return distance;
}
public void setDistance(Distance distance) {
this.distance = distance;
}
public Duration getDuration() {
return duration;
}
public void setDuration(Duration duration) {
this.duration = duration;
}
}
public static class Distance {
private String text;
private Integer value;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Integer getValue() {
return value;
}
public void setValue(Integer value) {
this.value = value;
}
}
public static class Duration {
private String text;
private Integer value;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Integer getValue() {
return value;
}
public void setValue(Integer value) {
this.value = value;
}
}
}
GeoZipCodesBean2 geoZipCodesBean2 = new Gson().fromJson(str, GeoZipCodesBean2.class);
This should be the JSON format for your GeoZipCodesBean2 object (if rows is a List<List<Elem>>)
{
"rows": [
[
{
"elements": [
{
"distance": {
"text": "897 mi",
"value": 1443464
},
"duration": {
"text": "14 hours 32 mins",
"value": 52327
},
"status": "OK"
}
]
},
{
"elements": [
{
"distance": {
"text": "378 mi",
"value": 607670
},
"duration": {
"text": "6 hours 22 mins",
"value": 22908
},
"status": "OK"
}
]
}
]
]
}
This is the code for converting to/from json
public static void main(String[] args) {
Gson gson = new GsonBuilder().create();
GeoZipCodesBean2 geo = new GeoZipCodesBean2();
List<List<Elem>> rows = new ArrayList<List<Elem>>();
List<Elem> elem = new ArrayList<Elem>();
Elem e1 = new Elem();
Distance d = new Distance();
d.setText("fads");
d.setValue(1234);
e1.setDistance(d);
elem.add(e1);
rows.add(elem);
geo.setRows(rows);
String json = gson.toJson(geo);
//The following prints {"rows":[[{"distance":{"text":"fads","value":1234}}]]}
System.out.println(json);
json = "{\"rows\":[[{\"distance\":{\"text\":\"fads\",\"value\":1234}, \"status\":\"OK\"}]]}";
GeoZipCodesBean2 geo2 = gson.fromJson(json, GeoZipCodesBean2.class);
//The following prints 1234
System.out.println(geo2.getRows().get(0).get(0).getDistance().getValue());
}