Vivart
Vivart

Reputation: 15313

How to parse a JSON response when type is also coming in response

I am getting following json response from a web service call. as you can see, what type of value we will get in response is also coming in type object.

 {"settings":[
    {
    "name":"name1",
    "value":4,
    "type":"int"
    },
    {
    "name":"name2",
    "value":false,
    "type":"boolean"
    },
    {
    "name":"name3",
    "type":"array",
    "value":[
    {
    "name":"name3"
    }]}]}
  1. how to parse this json?
  2. how to store parsed value in database where i have a table with column names name, value, etc?

Edit:

currently i am converting all values to string because we can't add boolean to database.

private enum Type{
    INT("int"), BOOLEAN("boolean"), ARRAY("array"),UNKNOWN_TYPE("");

    private String mType;
    Type(String type){
        mType = type;
    }

    public static Type toEnum(String type){
        for (Type value: Type.values()){
            if(value.mType.equals(type)){
                return value;
            }
        }
        return UNKNOWN_TYPE;
    }
}



                String value = null;
                switch (Type.toEnum(type)){
                    case INT:
                        value = String.valueOf(setting.getInt("value"));
                        break;
                    case BOOLEAN:
                        value = String.valueOf(setting.getBoolean("value"));
                        break;
                    case ARRAY:
                        parseJsonArray();
                        break;

                }

is this the correct approach?

Upvotes: 2

Views: 221

Answers (1)

Quuxplusone
Quuxplusone

Reputation: 27290

The usual way to deal with data items which could be any of a small known number of types is to use a tagged union. In Java, you'd write one something like this:

// CREATE TABLE dataFromJson (type ENUM('INT', 'BOOLEAN', 'STRING'),
//                            intval INT, boolval INT, stringval LONGTEXT);

class DataItem {
    public enum Type { INT, BOOLEAN, STRING };
    public Type m_type;
    public int m_int;
    public bool m_boolean;
    public String m_string;
    public PreparedStatement toInsertQuery(Connection conn) {
        PreparedStatement ps = conn.prepareStatement("INSERT INTO dataFromJson VALUES (?, ?, ?, ?)");
        ps.setString(1, m_type.toString());
        if (m_type==INT) ps.setInt(2, m_int); else ps.setObject(2, null);
        if (m_type==BOOLEAN) ps.setBoolean(3, m_boolean); else ps.setObject(3, null);
        if (m_type==STRING) ps.setString(4, m_string); else ps.setObject(4, null); 
        return ps;
    }
}

Dealing with JSON arrays (and objects) is much trickier; first you'll have to figure out how you want the data to be represented. Do you want the whole array as a string? do you want the first N elements of the array "exploded" into individual columns? do you want to store a single integer array_id, the primary key of a separate and more complicated table ArrayValues? There's all sorts of things you could do here... none of them terribly satisfying on a philosophical level. It depends on what you're going to want to do with the data later.

Upvotes: 1

Related Questions