Maxim Shoustin
Maxim Shoustin

Reputation: 77904

How to convert Json String with dynamic fields to Object?

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.

Upvotes: 9

Views: 16750

Answers (3)

rahulnikhare
rahulnikhare

Reputation: 1486

Please use this link for converting SON Response to Java POJO class http://www.jsonschema2pojo.org/

Upvotes: 0

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279940

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"
    }, ...
  }
}

Upvotes: 7

karmanaut
karmanaut

Reputation: 628

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.

Upvotes: 2

Related Questions