【问题标题】:parse recursively unknown json input structure in java在java中解析递归未知的json输入结构
【发布时间】:2014-04-02 10:34:39
【问题描述】:

我正在尝试解析 java 中递归未知的 json 输入结构,如下面的格式,并尝试在另一个 json 中重写相同的结构。

同时,我需要在解析时验证每个 json 键/值。

{"Verbs":[{
    "aaaa":"30d", "type":"ed", "rel":1.0, "id":"80", "spoken":"en", "ct":"on", "sps":null
},{
    "aaaa":"31", "type":"cc", "rel":3.0, "id":"10", "spoken":"en", "ct":"off", "sps":null
},{
    "aaaa":"81", "type":"nn", "rel":3.0, "id":"60", "spoken":"en", "ct":"on", "sps":null
}]}

请建议我可以使用哪个 json 解析器来读取和写入未知的 json 内容。

【问题讨论】:

  • 任何。它的json;他们解析它。
  • 如何解析每个 json 元素并将其重组为与输入相同
  • 你想对解析后的数据做什么?
  • 我想验证每个 & 每个 json 令牌键/值的特殊字符。然后我将替换值并将 json 重新构建回旧结构
  • (不要使用 Jackson 或任何其他“我们将为您构建 POJO”工具,除非结构相当规则且可重复。除非您真正理解它们,否则它们会比它们造成更多的混乱'值得。)

标签: java json recursion jackson gson


【解决方案1】:

这样可以递归解析JSON对象:

import com.eclipsesource.json.JsonArray;
import com.eclipsesource.json.JsonObject;
import com.eclipsesource.json.JsonValue;

public class JsonQuestion {

    public static void main(String[] args) {
        String input =  "{\"Verbs\":[{\n" +
                "    \"aaaa\":\"30d\", \"type\":\"ed\", \"rel\":1.0, \"id\":\"80\", \"spoken\":\"en\", \"ct\":\"on\", \"sps\":null\n" +
                "},{\n" +
                "    \"aaaa\":\"31\", \"type\":\"cc\", \"rel\":3.0, \"id\":\"10\", \"spoken\":\"en\", \"ct\":\"off\", \"sps\":null\n" +
                "},{\n" +
                "    \"aaaa\":\"81\", \"type\":\"nn\", \"rel\":3.0, \"id\":\"60\", \"spoken\":\"en\", \"ct\":\"on\", \"sps\":null\n" +
                "}]}";

        JsonObject jsonObject = JsonObject.readFrom(input);
        handleObject(jsonObject);
    }

    private static void handleValue(JsonObject.Member member, JsonValue value) {
        if (value.isArray()) {
            if (member != null) {
                System.out.print("name = " + member.getName());
            }
            System.out.println("array value ");
            recurseArray(value.asArray());
        } else if (value.isBoolean()) {
            if (member != null) {
                System.out.print("name = " + member.getName());
            }
            System.out.println(", boolean value = " + value.asBoolean());
        } else if (value.isNull()) {
            if (member != null) {
                System.out.print("name = " + member.getName());
            }
            System.out.println(", null value");
        } else if (value.isNumber()) {
            if (member != null) {
                System.out.print("name = " + member.getName());
            }
            System.out.println(", number value = " + value.asDouble());
        } else if (value.isObject()) {
            if (member != null) {
                System.out.print("name = " + member.getName());
            }
            System.out.println(", object value ");
            handleObject(value.asObject());
        } else if (value.isString()) {
            if (member != null) {
                System.out.print("name = " + member.getName());
            }
            System.out.println(", string value = " + value.asString());
        }
    }

    private static void handleObject(JsonObject object) {
        for (JsonObject.Member next : object) {
            JsonValue value = next.getValue();
            handleValue(next, value);
        }
    }

    private static void recurseArray(JsonArray array) {
        for (JsonValue value : array) {
            handleValue(null, value);
        }
    }
}

【讨论】:

  • 感谢您的回复,Parallely 我们可以在解析每个值的同时构建 json 响应。
  • 对不起,我不明白你的评论,是问题吗?
  • 是的,我们可以在 gson/jackson 库中实现相同的解析逻辑吗?因为我们只在我的项目中使用 gson/jackson 库,
  • 我想是的。但我用这个。
  • 最好在有人花时间帮助您之前提前指定 COMPLETE 要求。
【解决方案2】:

使用 gson 库 https://sites.google.com/site/gson/gson-user-guide

public void parseJson() {
     String jsonStr = "";//input json String.
     JsonParser parser = new JsonParser();
     JsonElement jsonElement = parser.parse(jsonStr);
     processJsonElement(jsonElement);
}


private void processJsonElement(JsonElement e) {
    if (e.isJsonArray()) {
        processJsonArray(e.getAsJsonArray());
    } else if (e.isJsonNull()) {
        processJsonNull(e.getAsJsonNull());
    } else if (e.isJsonObject()) {
        processJsonObject(e.getAsJsonObject());
    } else if (e.isJsonPrimitive()) {
        processJsonPrimitive(e.getAsJsonPrimitive());
    }
}

private void processJsonArray(JsonArray a) {
    for (JsonElement e : a) {
        processJsonElement(e);
    }
}

private void processJsonNull(JsonNull n) {
    System.out.println("null || : " + n);
}

private void processJsonObject(JsonObject o) {
    Set<Map.Entry<String, JsonElement>> members= o.entrySet();
    for (Map.Entry<String, JsonElement> e : members) {
        System.out.println("Processing object member: " + e.getKey());
        processJsonElement(e.getValue());
    }
}

private void processJsonPrimitive(JsonPrimitive p) {
    System.out.println("Primitive || :" + p);
}

或者杰克逊

public void processJson() {
    ObjectMapper objectMapper = new ObjectMapper();
    try {
        JsonNode node = objectMapper.readTree(jsonStr);
        System.out.println(node);
        processNode(node);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

}

private void processNode(JsonNode n) {
    if (n.isContainerNode()) {
        processJsonContainer(n.iterator());
    } else if (n.isNull()) {
        System.out.println("Null || :" + n);
    } else if (n.isNumber()) {
        System.out.println("Number || :" + n.asDouble());
    } else if (n.isBoolean()) {
        System.out.println("Boolean || :" + n.asBoolean());
    } else if (n.isTextual()) {
        System.out.println("Text || :" + n.asText());
    }
}

private void processJsonContainer(Iterator<JsonNode> iterator) {
   while (iterator.hasNext()) {
       processNode(iterator.next());
   }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-05
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    • 2016-10-17
    • 1970-01-01
    相关资源
    最近更新 更多