How to analyze JSON data with the same key and different data?

advertisements

I have a little problem with parse JSON data.

There have a Web API,basic response data like this:

{
    topic_name: "Kevin",
    topic_type: 1,
    extraData: {}
}

With the different topic_type value, the extraData maybe have different data structure, e.g the extraData object has different key-values.

In this case, how to create model classes and parse the JSON string to models?

Or does this API design reasonable? Is there a better API design to solve these cases?

update 1:

With the same topic_type, the extraData's structure is always same.

I have considered use subclasses, but it need a subclass for every topic_type.

update 2:

Here is some example of JSON data, different topic_type with different extraData.

when topic_type equal to 1,

{
    topic_name: "Kevin",
    topic_type: 1,
    extraData: {
        data_type1: value,
        data_type2: value2
    }
}

when topic_type equal to 2,

{
    topic_name: "David",
    topic_type: 2,
    extraData: {
        data_type3: value3
    }
}

it not real data, I'm not deal with a 'topic' issue, just a example, the key is the extraData object has different type keys.


JSONModel might be exactly what you're looking for. Parses json and gives you models