简体   繁体   English

Android中的通用JSON解析

[英]Generic JSON parsing in Android

I have a JSON 我有一个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. 只有“ info”和“ data”标签将固定不变,其余所有都可以更改。 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. 我们可以有空的“ info”,也可以有不同的子级,例如“ info1”,“ info2”。...“ info5” ...类似地,单个“ info”子级可以有多个子级,例如“ info1”可以有2个条目或4个条目。

I tried using jackson library, but not able to traverse the complete json. 我尝试使用杰克逊库,但无法遍历完整的json。

UPDATE : using jackson 2.7.2 (latest) Used following code 更新 :使用杰克逊2.7.2(最新)使用以下代码

    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. 迭代了“ info”和“ data”键。 Need to iterate over complete json. 需要遍历完整的json。

JsonNode can be used to parse whole jsonObject. JsonNode可用于解析整个jsonObject。 JsonNode get method can be used to traverse the given JSON. JsonNode get方法可用于遍历给定的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. 为什么不尝试GSON。

https://github.com/google/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 您需要做的就是使用gson序列化数据,如下所示

         @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 使用以下代码从json获取对象

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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