0

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.

6
  • Do you know the extra fields that you are getting for each topic_type? Are they always the same for each topic_type? You can decide between subclassing or use only one class. Commented Sep 15, 2016 at 8:29
  • This post will help you: stackoverflow.com/questions/38842704/… Commented Sep 15, 2016 at 8:32
  • can you show some examples for different data models for extraData ? Commented Sep 15, 2016 at 10:21
  • @UmairAfzal updated. Thank you. Commented Sep 15, 2016 at 10:39
  • @crom87 yes, same topic_type always has same extraData structure. Commented Sep 15, 2016 at 10:40

2 Answers 2

0

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

Sign up to request clarification or add additional context in comments.

9 Comments

I know this awesome project, in my impression this project only can parse static model type with static JSON structure, in my case, the extraData's structure is dynamic depend on topic_type. Does JSONModel work with this issue?
You could create an extraData model that has optional properties and it should create the extraData model with the content you're looking for. I've used it in the past for models with changing content from the json
Do you mean create a extraModel with all possible keys?
Yes or make 2 different models and look to generate the different ones based on the topic_type
If only create one extraData model, the model will has to many useless keys and all of them equal to nil, maybe it's a bit ugly?
|
0

try to define all types in a class:

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

after Json parsing, take the data types u need based on topic type.

Ps. All data types need to be nullable

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.