简体   繁体   中英

Generic JSON parsing in Android

I have a JSON

 {
    "info": {
        "info1": {
            "age": "30",
            "city": "New york"
        },
        "info2": {
            "sleeping": "false"
        },
        "info3": {
            "shopping": "false",
            "eating": "Buger"
        }
    },
    "data": [{
        "name": "XYZ",
        "email": "xyz@123.com"
    }, {
        "name": "ABC",
        "email": "ABC@123.com"
    }]
}

I need to make a parser that would be generic, and extract names for objects, arrays and individual key pair text. I will be generating a query using these values. Only "info" and "data" tags will be fixed rest all can change. We can have empty "info" or different children like "info1", "info2".... "info5" ... Similarly, individual "info" child can have multiple children like "info1" can have 2 entries or 4 entries.

I tried using jackson library, but not able to traverse the complete json.

UPDATE : using jackson 2.7.2 (latest) Used following code

    JsonFactory factory = new JsonFactory();

    ObjectMapper mapper = new ObjectMapper(factory);
    JsonNode rootNode = mapper.readTree(jsonString);

    Iterator<Map.Entry<String, JsonNode>> fieldsIterator = rootNode.fields();
    while (fieldsIterator.hasNext()) {

        Map.Entry<String, JsonNode> field = fieldsIterator.next();
        System.out.println("Key: " + field.getKey() + "\tValue:" + field.getValue());
    }

It iterated for the "info" and "data" key. Need to iterate over complete json.

JsonNode can be used to parse whole jsonObject. JsonNode get method can be used to traverse the given JSON.

eg :

ObjectMapper mapper = new ObjectMapper();
JsonNode rootNode = mapper.readTree(data);

JsonNode infoNode = rootNode.get("info");

Iterator<Map.Entry<String, JsonNode>> infoFieldsIterator = infoNode.fields();
while (infoFieldsIterator.hasNext()) {

    Map.Entry<String, JsonNode> field = infoFieldsIterator.next();
    System.out.println("Key: " + field.getKey() + "\tValue:" + field.getValue());
}

JsonNode dataNode = rootNode.get("data");

for (int i = 0; i < dataNode.size(); i++) {
    JsonNode dataNodeNum = dataNode.get(i);
    Iterator<Map.Entry<String, JsonNode>> dataFieldsIterator = dataNodeNum.fields();
    while (dataFieldsIterator.hasNext()) {
        Map.Entry<String, JsonNode> field = dataFieldsIterator.next();
        System.out.println("Key: " + field.getKey() + "\tValue:" + field.getValue());
    }
}

For more info please check here .

Why not try GSON.

https://github.com/google/gson

You can use models inside models to achieve what you need.

ALL you need to do is serialise data using gson as follows

         @SerializedName("info")
           private infomodel info;

you can create a seperate model called info model which contains serialized name for its objects.

use the below code to get object from json

        Gson gson=new Gson();
        Model yourModel=gson.fromJson(<your json object as string>);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM