我有一个使用Gson在下面使用
创建的Jsonval gson = new Gson
val jsonString = gson.toJson(mydata).
println(jsonString)
这是输出
{"mydata":{"date":["2008-06-04T22:47:36Z"],
"pdf:PDFVersion":["1.6"],
"pdf:docinfo:title":[" PDF Test Page"],
"xmp:CreatorTool":["Acrobat PDFMaker 7.0.7 for Word"],
"Company":["Government of Yukon"],
"access_permission:modify_annotations":["false"],
"access_permission:can_print_degraded":["true"],
"dc:creator":["Yukon","Canada","Yukon Department of Education"],
"language":["EN-US"],
"dcterms:created":["2008-06-04T22:44:00Z"],
"Last-Modified":["2008-06-04T22:47:36Z"],
"dcterms:modified":["2008-06-04T22:47:36Z"],
"dc:format":["application/pdf; version\u003d1.6"],
"title":[" PDF Test Page"],
}
}
所需的输出
{"date":["2008-06-04T22:47:36Z"],
"pdf:PDFVersion":["1.6"],
"pdf:docinfo:title":[" PDF Test Page"],
"xmp:CreatorTool":["Acrobat PDFMaker 7.0.7 for Word"],
"Company":["Government of Yukon"],
"access_permission:modify_annotations":["false"],
"access_permission:can_print_degraded":["true"],
"dc:creator":["Yukon","Canada","Yukon Department of Education"],
"language":["EN-US"],
"dcterms:created":["2008-06-04T22:44:00Z"],
"Last-Modified":["2008-06-04T22:47:36Z"],
"dcterms:modified":["2008-06-04T22:47:36Z"],
"dc:format":["application/pdf; version\u003d1.6"],
"title":[" PDF Test Page"],
}
我想知道我如何能够删除上述json中的标头({“ mydata” :)和页脚(}),以便我可以用来向Solr添加记录
答案 0 :(得分:1)
可以通过以下方式使用Gson的API移除包装器mydata
:将初始对象转换为JSON树(而不是问题中尝试的字符串),然后将mydata
节点的内容转换为JSON字符串。这很容易:
val jsonTree = gson.toJsonTree(mydata)
val mydataNode = jsonTree.getAsJsonObject().getAsJsonObject("mydata")
val jsonString = gson.toJson(mydataNode) // jsonString contains the desired output
P.S。应该删除原始json中,
之后的逗号"title":[" PDF Test Page"],
,否则无效。