我试图在Java中解析JSON字符串,并找到键值对,以便确定JSON对象的大致 struct ,因为JSON字符串的对象 struct 未知.

例如,一次执行可能会有如下JSON字符串:

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

And another like this:

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

如何循环使用各种JSON元素并确定键及其值?我看了jackson-coreJsonParser分.我知道如何获取下一个"令牌",并确定它是什么类型的令牌(即字段名、值、数组开始等),但我不知道如何获取实际令牌的值.

For example:

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)) {
           logger.debug("Value True : " + token.toString());
        } else {
           logger.debug("Something else : " + token.toString());
        }
        token = parser.nextToken();
     }
  } catch (Exception e) {
     logger.error("", e);
  }
}

Is there a class in jackson or some other library (gson or simple-json) that produces a tree, or allows one to cycle through the json elements and obtain the actual key names in addition to the values?

推荐答案

看看Jacksons built-in tree model feature.

And your code will be:

public void parse(String json)  {
       JsonFactory factory = new JsonFactory();

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

       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());
       }
}

Json相关问答推荐

将数据从嵌套的SON数组提取到更简单的数组中

JOLT拉平数组

您可以使用Jolt对JSON执行OR条件吗

创建Json嵌套文件 struct

将部分数据字节解组到自定义 struct 中

JOLT分裂和数组数据

如何加入或合并列表元素列表(未知长度)

Shell脚本空格转义

将 json 文件转换为 json 对象会打乱对象的顺序

如何判断 Json 对象中是否存在键并获取其值

没有很多类的 GSON 解析

Json.Net:用于自定义命名的 JsonSerializer-Attribute

将错误消息作为 JSON 对象发送

Golang struct 的 XML 和 JSON 标签?

将 CoffeeScript 项目转换为 JavaScript(不缩小)?

Swift :将 struct 转换为 JSON?

as_json 没有在关联上调用 as_json

严重:找不到媒体类型 = 应用程序/json、类型 = 类 com.jersey.jaxb.Todo、通用类型 = 类 com.jersey.jaxb.Todo 的 MessageBodyWriter

python追加到json对象中的数组

将多个值存储在json中的单个键中