问题描述
如何使用jackson创建如下例所示的json数组。
我尝试使用ObjectMapper,但这看起来不正确。
try(DirectoryStream< Path> ds = Files.newDirectoryStream(path)){
for(Path file:ds ){
System.out.println(name:+ file.getFileName()+
\\\
+
mime:+ Files.probeContentType(file)+
\\\
+
locked:+!Files.isWritable(file));
}
} catch(IOException e){
System.err.println(e);
}
最后,我将制作一个具有以下值的json。
* - (int)b中的文件大小。在unix时间需要
* - (int)ts文件修改时间。需要
* - (字符串)mime mimetype。文件夹需要,其他 - 可选
* - (bool)读取读取权限。需要
* - (bool)写入写入权限。所需的
* - (bool)锁定是对象锁定的。可选
* - (bool)hidden隐藏对象。可选
* - (字符串)符号链接的别名 - 链接相对于根路径的目标路径。可选
* - (字符串)符号链接目标 - 链接目标路径。可选
以下是我提供的示例json。
files:[
{
mime:directory,
ts:1334071677,
读:1,
写:0,
size:0,
hash:l1_Lw,
volumeid:l1_,
name:Demo,
locked:1,
dirs:1
},
{
mime:目录,
ts:1334071677,
读:1,
写:0,
size:0,
hash :l1_Lw,
volumeid:l1_,
name:Demo,
locked:1,
dirs:1
$ m $ mime $目录
$ ts 1340114567
$ read $:
写入0: ,
size:0,
hash:l1_QmFja3Vw,
name:Backup,
phash:l1_Lw,
锁定:1
$ m $ mime $目录
$ ts $ 1310252178
读取1
写入0,
size:0,
hash:l1_SW1hZ2Vz,
name:Images,
phash:l1_Lw,
locked:1
},
{
mime:application \ / x-genesis-rom,
ts:1310347586,
read:1,
write:0,
size:3683,
hash:l1_UkVBRE1FLm1k,
name:README.md ,
phash:l1_Lw,
locked:1
}
]
编辑1
映射< String,Object> filesMap = new HashMap<>();
列表< Object> files = new ArrayList< Object>();
System.out.println(\\\
No filter applied:); (路径文件:ds){
Map< String,Object>
try(DirectoryStream< Path> ds = Files.newDirectoryStream(path)){ fileInfo = new HashMap<>();
fileInfo.put(name,file.getFileName()。toString());
//在Director中打印文件
// Files.getAttribute(file,size);
System.out.println(name:+ file.getFileName()。toString()+
\\\
+
mime:+ Files.probeContentType(file) +
\\\
+
locked:+!Files.isWritable(file));
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(fileInfo);
files.add(json);
}
} catch(IOException e){
System.err.println(e);
}
files.toArray();
filesMap.put(files,files);
ObjectMapper mapper = new ObjectMapper();
字符串jsonString;
尝试{
jsonString = mapper.writeValueAsString(filesMap);
} catch(IOException e){
jsonString =fail; //改变catch语句的主体使用File |设置|文件模板。
}
下面的json比较接近,但我无法弄清楚为什么在{}之前和之后多加引号。
{files:[{\name \ :\ 32C92124-EFCF-42C1-AFD2-8B741AE6854B.jpg\}, {\ name\ :\ 58D5B83F-4065-4D6E-92BE-8181D99CB6CB.jpg\ } , {\ name\ :\ 7B1464A0-FBA1-429E-8A39-3DE5B539FBF8.jpg\ }, {\ name\ :\ 888159CF-45BE-475F- 8C6A-64B3E1D97278.jpg \}]}
最终答案
地图< String,Object> filesMap = new HashMap<>();
列表< Object> files = new ArrayList< Object>();
System.out.println(\\\
No filter applied:); (路径文件:ds){
Map< String,Object>
try(DirectoryStream< Path> ds = Files.newDirectoryStream(path)){ fileInfo = new HashMap<>();
fileInfo.put(name,file.getFileName()。toString());
System.out.println(name:+ file.getFileName()。toString()+
\\\
+
mime:+ Files.probeContentType(file) +
\\\
+
locked:+!Files.isWritable(file));
files.add(fileInfo);
}
} catch(IOException e){
System.err.println(e);
}
files.toArray();
filesMap.put(files,files);
ObjectMapper mapper = new ObjectMapper();
字符串jsonString;
尝试{
jsonString = mapper.writeValueAsString(filesMap);
} catch(IOException e){
jsonString =fail;
您需要一个:
final JsonNodeFactory factory = JsonNodeFactory.instance;
这个类有创建 ArrayNode
s , ObjectNode
s, IntNode
s, DecimalNode
s, TextNode
s和whatnot。 ArrayNode
s和 ObjectNode
具有方便的变异方法,可直接添加大多数JSON基元(非容器)值,而无需去通过工厂(在内部,他们引用这个工厂,这就是为什么)。
对于 ObjectMapper
,注意它是一个序列化器( ObjectWriter
)和解串器( ObjectReader
)。
How can I create a json array like the example below using jackson.
I tried using ObjectMapper, but this does not seem correct.
try (DirectoryStream<Path> ds = Files.newDirectoryStream(path)) {
for (Path file : ds) {
System.out.println("name:"+file.getFileName()+
"\n"+
"mime:"+Files.probeContentType(file)+
"\n"+
"locked:"+!Files.isWritable(file));
}
} catch (IOException e) {
System.err.println(e);
}
Eventually I will be making a json that has the below values.
* - (int) size file size in b. required
* - (int) ts file modification time in unix time. required
* - (string) mime mimetype. required for folders, others - optionally
* - (bool) read read permissions. required
* - (bool) write write permissions. required
* - (bool) locked is object locked. optionally
* - (bool) hidden is object hidden. optionally
* - (string) alias for symlinks - link target path relative to root path. optionally
* - (string) target for symlinks - link target path. optionally
Here is an example json I was provided.
"files": [
{
"mime": "directory",
"ts": 1334071677,
"read": 1,
"write": 0,
"size": 0,
"hash": "l1_Lw",
"volumeid": "l1_",
"name": "Demo",
"locked": 1,
"dirs": 1
},
{
"mime": "directory",
"ts": 1334071677,
"read": 1,
"write": 0,
"size": 0,
"hash": "l1_Lw",
"volumeid": "l1_",
"name": "Demo",
"locked": 1,
"dirs": 1
},
{
"mime": "directory",
"ts": 1340114567,
"read": 0,
"write": 0,
"size": 0,
"hash": "l1_QmFja3Vw",
"name": "Backup",
"phash": "l1_Lw",
"locked": 1
},
{
"mime": "directory",
"ts": 1310252178,
"read": 1,
"write": 0,
"size": 0,
"hash": "l1_SW1hZ2Vz",
"name": "Images",
"phash": "l1_Lw",
"locked": 1
},
{
"mime": "application\/x-genesis-rom",
"ts": 1310347586,
"read": 1,
"write": 0,
"size": 3683,
"hash": "l1_UkVBRE1FLm1k",
"name": "README.md",
"phash": "l1_Lw",
"locked": 1
}
]
EDIT 1
Map<String, Object> filesMap = new HashMap<>();
List<Object> files = new ArrayList<Object>();
System.out.println("\nNo filter applied:");
try (DirectoryStream<Path> ds = Files.newDirectoryStream(path)) {
for (Path file : ds) {
Map<String, Object> fileInfo = new HashMap<>();
fileInfo.put("name", file.getFileName().toString());
// Prints Files in Director
// Files.getAttribute(file,"size");
System.out.println("name:" + file.getFileName().toString() +
"\n" +
"mime:" + Files.probeContentType(file) +
"\n" +
"locked:" + !Files.isWritable(file));
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(fileInfo);
files.add(json);
}
} catch (IOException e) {
System.err.println(e);
}
files.toArray();
filesMap.put("files", files);
ObjectMapper mapper = new ObjectMapper();
String jsonString;
try {
jsonString = mapper.writeValueAsString(filesMap);
} catch (IOException e) {
jsonString = "fail"; //To change body of catch statement use File | Settings | File Templates.
}
Puts out the following json which is closer, but I can't figure out why the extra quotes before and after the {}.
{"files":["{\"name\":\"32C92124-EFCF-42C1-AFD2-8B741AE6854B.jpg\"}","{\"name\":\"58D5B83F-4065-4D6E-92BE-8181D99CB6CB.jpg\"}","{\"name\":\"7B1464A0-FBA1-429E-8A39-3DE5B539FBF8.jpg\"}","{\"name\":\"888159CF-45BE-475F-8C6A-64B3E1D97278.jpg\"}"]}
Final Answer
Map<String, Object> filesMap = new HashMap<>();
List<Object> files = new ArrayList<Object>();
System.out.println("\nNo filter applied:");
try (DirectoryStream<Path> ds = Files.newDirectoryStream(path)) {
for (Path file : ds) {
Map<String, Object> fileInfo = new HashMap<>();
fileInfo.put("name", file.getFileName().toString());
System.out.println("name:" + file.getFileName().toString() +
"\n" +
"mime:" + Files.probeContentType(file) +
"\n" +
"locked:" + !Files.isWritable(file));
files.add(fileInfo);
}
} catch (IOException e) {
System.err.println(e);
}
files.toArray();
filesMap.put("files", files);
ObjectMapper mapper = new ObjectMapper();
String jsonString;
try {
jsonString = mapper.writeValueAsString(filesMap);
} catch (IOException e) {
jsonString = "fail";
}
You need a JsonNodeFactory
:
final JsonNodeFactory factory = JsonNodeFactory.instance;
This class has methods to create ArrayNode
s, ObjectNode
s, IntNode
s, DecimalNode
s, TextNode
s and whatnot. ArrayNode
s and ObjectNode
s have convenience mutation methods for adding directly most JSON primitive (non container) values without having to go through the factory (well, internally, they reference this factory, that is why).
As to an ObjectMapper
, note that it is both a serializer (ObjectWriter
) and deserializer (ObjectReader
).
这篇关于使用jackson创建一个json对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!