在不知道JSON格式的情况下以Java解析JSON

我试图解析Java中的JSON字符串并找到键值对,以便我可以确定JSON对象的近似结构,因为JSON字符串的对象结构是未知的。


例如,一个执行可能具有这样的JSON字符串:


  {"id" : 12345, "days" : [ "Monday", "Wednesday" ], "person" : { "firstName" : "David", "lastName" : "Menoyo" } }

另一个像这样:


  {"url" : "http://someurl.com", "method" : "POST", "isauth" : false }

如何循环遍历各种JSON元素并确定键及其值?我看着jackson-core的JsonParser。我知道如何获取下一个“令牌”并确定令牌的类型(即字段名称,值,数组开头等),但是,我不知道如何获取实际令牌的值。


例如:


public void parse(String json)  {

  try {

     JsonFactory f = new JsonFactory();

     JsonParser parser = f.createParser(json);

     JsonToken token = parser.nextToken();

     while (token != null) {

        if (token.equals(JsonToken.START_ARRAY)) {

           logger.debug("Start Array : " + token.toString());

        } else if (token.equals(JsonToken.END_ARRAY)) {

           logger.debug("End Array : " + token.toString());

        } else if (token.equals(JsonToken.START_OBJECT)) {

           logger.debug("Start Object : " + token.toString());

        } else if (token.equals(JsonToken.END_OBJECT)) {

           logger.debug("End Object : " + token.toString());

        } else if (token.equals(JsonToken.FIELD_NAME)) {

           logger.debug("Field Name : " + token.toString());

        } else if (token.equals(JsonToken.VALUE_FALSE)) {

           logger.debug("Value False : " + token.toString());

        } else if (token.equals(JsonToken.VALUE_NULL)) {

           logger.debug("Value Null : " + token.toString());

        } else if (token.equals(JsonToken.VALUE_NUMBER_FLOAT)) {

           logger.debug("Value Number Float : " + token.toString());

        } else if (token.equals(JsonToken.VALUE_NUMBER_INT)) {

          logger.debug("Value Number Int : " + token.toString());

        } else if (token.equals(JsonToken.VALUE_STRING)) {

           logger.debug("Value String : " + token.toString());

        } else if (token.equals(JsonToken.VALUE_TRUE)) {


jackson或其他库(gson或simple-json)中是否有一个类可以生成树,或者允许一个类循环遍历json元素并获取值之外的实际键名?


富国沪深
浏览 1081回答 3
3回答

慕慕森

如果其他库适合您,则可以尝试org.json:JSONObject object = new JSONObject(myJSONString);String[] keys = JSONObject.getNames(object);for (String key : keys){    Object value = object.get(key);    // Determine type of value and do something with it...}

胡子哥哥

查找以下代码,以使用Gson库解析未知的Json对象。public class JsonParsing {static JsonParser parser = new JsonParser();public static HashMap<String, Object> createHashMapFromJsonString(String json) {&nbsp; &nbsp; JsonObject object = (JsonObject) parser.parse(json);&nbsp; &nbsp; Set<Map.Entry<String, JsonElement>> set = object.entrySet();&nbsp; &nbsp; Iterator<Map.Entry<String, JsonElement>> iterator = set.iterator();&nbsp; &nbsp; HashMap<String, Object> map = new HashMap<String, Object>();&nbsp; &nbsp; while (iterator.hasNext()) {&nbsp; &nbsp; &nbsp; &nbsp; Map.Entry<String, JsonElement> entry = iterator.next();&nbsp; &nbsp; &nbsp; &nbsp; String key = entry.getKey();&nbsp; &nbsp; &nbsp; &nbsp; JsonElement value = entry.getValue();&nbsp; &nbsp; &nbsp; &nbsp; if (null != value) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!value.isJsonPrimitive()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (value.isJsonObject()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map.put(key, createHashMapFromJsonString(value.toString()));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (value.isJsonArray() && value.toString().contains(":")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<HashMap<String, Object>> list = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JsonArray array = value.getAsJsonArray();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (null != array) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (JsonElement element : array) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.add(createHashMapFromJsonString(element.toString()));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map.put(key, list);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (value.isJsonArray() && !value.toString().contains(":")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map.put(key, value.getAsJsonArray());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map.put(key, value.getAsString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return map;}}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java