Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Json String with dynamic fields to Object?

Tags:

java

json

I have the followed snipets of Json String:

 {
  "networks": {
    "tech11": {
        "id": "1",
        "name": "IDEN"
    },
    "tech12": {
        "id": "2",
        "name": "EVDO_B"
    }
 }
}

I use some methods to convert this String to Object:

    private static Gson mGson = new Gson();

    ...

public static WebObjectResponse convertJsonToObject(String jsonString) {

    WebObjectResponse webObjectResponse = null;


    if(jsonString != null && jsonString.length() > 1){
        webObjectResponse = mGson.fromJson(jsonString, WebObjectResponse.class);
    }

    return webObjectResponse;

}

Where WebObjectResponse is class that should represent above mentioned String.

Its not complicated if I get static fields. But in my case the values have different names: tech11, tech12 ....

I can use @SerializedName but its works in specific cases like convert "class" to "class_". As you see networks Object defined as list of tech Objects but with different post-fix.

public class WebObjectResponse{
 private DataInfoList networks = null;
} 

This is static implementation, i defined 2 values tech11 and tech12 but next response might be techXX

public class DataInfoList {
 private DataInfo tech11 = null;
 private DataInfo tech12 = null;
}


public class DataInfo {
 private String id = null;
 private String name = null;
}

What is the good way to convert current Json String to Object where list of elements are Objects too and have different names?

Thank you.

like image 874
Maxim Shoustin Avatar asked Mar 13 '13 15:03

Maxim Shoustin


2 Answers

Use a Map!

I would do the following

public class WebObjectResponse {
     private Map<String, DataInfo> networks;
} 

public class DataInfo {
     private String id = null;
     private String name = null;
}

// later
Gson gson = new Gson();
String json = "{\"networks\": {\"tech11\": { \"id\": \"1\",\"name\": \"IDEN\" },  \"tech12\": { \"id\": \"2\", \"name\": \"EVDO_B\" }    }}";

WebObjectResponse response = gson.fromJson(json, WebObjectResponse .class);

For each object in json networks, a new entry will be added to the Map field of your class WebObjectResponse. You then reference them by techXX or iterate through the keyset.

Assuming a structure like this

{
  "networks": {
    "tech11": {
        "id": "1",
        "name": "IDEN"
    },
    "tech12": {
        "id": "2",
        "name": "EVDO_B"
    },
    "tech13": {
        "id": "3",
        "name": "WOHOO"
    }, ...
  }
}
like image 62
Sotirios Delimanolis Avatar answered Oct 24 '22 20:10

Sotirios Delimanolis


We would need your class structure for more details.

As far as I am aware, I think you will need to have some mappings defined somewhere (I used xml's) and then try to match json with one of the mappings to create objects.

Google gson is good. I did it in Jackson

Also, converting objects should be trivial. But since you might have variable fields like tech11 and tech12 , you might want to store the "network" value as a string and then extract fields out of it when required.

Hope I could help.

Edit : Sotirious nails it.

like image 32
karmanaut Avatar answered Oct 24 '22 19:10

karmanaut