androidjsongson

A value of a JSON str sometimes is a String, sometimes is a object, how could I use gson to parse it


My json str sometimes like this:

{
    "data": {
        "changebaby": "no change",
        "changemama": {
            "mamacontext": "mama is a good mama",
            "mamaico": "",
            "mamatitle": "mama"
        }
    }
}

sometimes it like this:

{
    "data": {
        "changebaby": "no change",
        "changemama": "no change"
    }
}

As you see,the value of the "changebaby" key and the "changemama" key sometimes is a String, sometimes is a object, how should I parse it by gson?

Don't use the android api to parse the json string, need to use the gson lib of google to parse the JSON string.


Solution

  • To simplyfy android development, we can ask for the backend developers to change the Mobile API.The new API could returen the json string that cann't change the value.The value of all keys cann't sometimes be a string, sometimes a object.