0

I have two different Json responses(having different keys) generated out of two different requests :

Response 1 :

{
    "response": {
        "count": 2,
        "programs": [
            {
                "title": "xyz1",
                "desc": "ABCDEF1"
            },
            {
                "title": "xyz2",
                "desc": "ABCDEF2"
            }
        ]
    }
}

Response 2

{
    "response": {
        "count": 3,
        "shows": [
            {
                "name": "PQR1",
                "desc": "qwerty1"
            },
            {
                "name": "PQR2",
                "desc": "qwerty2"
            },
            {
                "name": "PQR3",
                "desc": "qwerty3"
            }
        ]
    }
}

As we can see the responses contain data with different keys. But Ultimately It could be transformed into (Array of) same Java object like this one:

Program {
   String title;
   int description;
}

I want to write single parsing logic that handles different key names and return Program list. How to achieve this efficiently?

Is there any library available to conveniently do this ?

Pavan
  • 711
  • 2
  • 6
  • 17
  • possible duplicate of [How to parse JSON in Java](http://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) – swalog Mar 24 '15 at 08:43
  • Have you looked at the other questions on stack overflow asking how to parse json with java? – swalog Mar 24 '15 at 08:44
  • I know ways to parse JSON data. What i want is different and I have updated my question explaining the same. – Pavan Mar 24 '15 at 08:58
  • Have you considered changing your `json` structure, to reflect different field names? E.g. to specify something like `"response_type": "programs"`, and then a `"data"` node with the contents. – swalog Mar 24 '15 at 09:03
  • I wont be having control over json structure in the response. It comes from a third party. – Pavan Mar 24 '15 at 09:09
  • The question is not about a generic way to handle JSON, since that would imply other mechanisms, like storing in a HahsMap or something. – tbc Mar 24 '15 at 10:22
  • Please help me in editing the question, so that it wont be marked as duplicate :P – Pavan Mar 24 '15 at 10:40

1 Answers1

3

You may choose the field in the getter when deserialized both of them (example works with GSON):

class Program {
    private String title, name;
    @SerializedName("desc") private String description;

    private String getTitle() {
        return title == null ? name : title;
    }

    // other getters, empty constructor and so on...
}

Also (again GSON), you can register your own TypeAdapter when creating Gson object.

// let Program have empty constructor (or no constructors at all), getters and setters
class ProgramAdapter extends TypeAdapter<Program> {

    @Override
    public Program read(final JsonReader in) throws IOException {
        final Program obj = new Program();

        in.beginObject();
        while (in.hasNext()) {
            String jsonTag = in.nextName();
            if ("desc".equals(jsonTag)) {
                obj.setDescription(in.nextString());
            } else if ("title".equals(jsonTag) 
                    || "name".equals(jsonTag)) {
                obj.setTitle(in.nextString());
            }
        }
        in.endObject();

        return obj;
    }

    @Override
    public void write(final JsonWriter out, final Program obj)
            throws IOException {
        out.beginObject();
        out.name("title").value(obj.getTitle());
        out.name("desc").value(obj.getDescription());
        out.endObject();
    }
}

// then, when create `Gson` object:

Gson gson = new GsonBuilder().registerTypeAdapter(Program.class, new ProgramAdapter()).create();
Dmitry Ginzburg
  • 7,391
  • 2
  • 37
  • 48
  • I got your point. But is there is way we can give expression like "name OR title". For example : @SerializedName("name|title") private String title; – Pavan Mar 24 '15 at 09:06
  • @Pavan, [no](http://google-gson.googlecode.com/svn/tags/1.2.3/docs/javadocs/com/google/gson/annotations/SerializedName.html), there's no such a way. Still, you can implement your own field naming policy. I can udpate my answer to reflect this possibility, if you need it. – Dmitry Ginzburg Mar 24 '15 at 09:11
  • Thanks, that would be helpful. :) – Pavan Mar 24 '15 at 09:25
  • Sorry, will do.. Before that, i will try the answer once :) – Pavan Mar 24 '15 at 10:42