如何在JsonArray中创建(键,值) [英] How create a (key, value) in JsonArray

查看:42
本文介绍了如何在JsonArray中创建(键,值)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSONObject,就像此链接中的输出一样:

I have a JSONObject, like the output in this link:

https://hadoop.apache.org/docs/r1.0.4/webhdfs.html#GETFILESTATUS

我希望在JSON数组中获取 pathSuffix (文件名)和 modificationTime (日期)值,如下所示:

I woul dlike to get the pathSuffix (file names) and the modificationTime (Dates) values in a JSON Array, like this:

[
  { file1, date
  },

  { file1, date
  },

  { file1, date
  },
.
.
.
]

我的代码如下:

JsonObject fileStatuses = jsonObject.getJsonObject("FileStatuses");
                    JsonArray fileStatus = (JsonArray) fileStatuses.getJsonArray("FileStatus");
                                                                     
                        for (int i = 0; i < fileStatus.size(); i++) {
                            
                            JsonObject rec = fileStatus.getJsonObject(i);                   
                            String pathSuffix = rec.getString("pathSuffix");
                            
                            
                            String  modificationTime = rec.getJsonNumber("modificationTime").toString();
                            long a = Long.parseLong(modificationTime);
                            Date modificationTimeDate = new Date(a);
                            
                            
                            JsonObject jo = Json.createObjectBuilder()
                                      .add("list", Json.createArrayBuilder()
                                        .add(Json.createObjectBuilder()
                                          .add(pathSuffix, (JsonValue) modificationTimeDate)
                                          ))
                                      .build();
                            logger.info("JSON object is '{}'", jo);

我遇到了这个异常:

Exception in thread "main" java.lang.ClassCastException: java.util.Date incompatible with javax.json.JsonValue

如何创建一个包含 Pathsuffix ModificationTime 值(如(key,value))的JsonArray?谢谢

How can I create a JsonArray that contain the values of Pathsuffix and ModificationTime like (key, value) ? Thanks

推荐答案

如果要存储JSON对象 filename:modificationTime ,则可以通过构造一个保存的Map来实现此目的.键对:

If you want to store a JSON-Object filename:modificationTime you can accomplish this by constructing a Map that holds your key and value pairs:

JsonArrayBuilder builder = Json.createArrayBuilder();
for (int i = 0; i < fileStatus.size(); i++) {

    JsonObject rec = fileStatus.getJsonObject(i);
    String timeStamp = rec.get("modificationTime").toString();
    Map<String, Object> jsonMapping = Map.of(rec.getString("pathSuffix"),
                timeStamp);
    builder.add(Json.createObjectBuilder(jsonMapping));
}

此后,对于您链接中的json, builder.build()将产生如下的 JsonArray :

After that, for the json from your Link, builder.build() will yield a JsonArray like this:

[
    {
        "a.patch":"1320171722771" // FileStatus.pathSuffix : FileStatus.modificationTime
    },
    {
        "bar":"1320895981256"
    }
]

除非,否则您需要根据我强烈建议的时间戳构造一个特定的日期,以按原样存储所获取的时间戳.这是因为从时间戳创建任何 LocalDate (或更糟糕的是: Date )都会产生不正确的日期,除非您考虑了两者的 TimeZone 发生文件修改,并读取了ModifyTime值.

Unless you need to construct a certain Date from the timestamp I stronlgy advice to store the acquired timestamp as is. This is because creating any LocalDate (or worse: Date) from a timestamp will yield inaccurate dates unless you consider the TimeZones of both where the file modification happened and where the modificationTime value is read.

这篇关于如何在JsonArray中创建(键,值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆