我正在从树模型转换为 Jackson 中的流式 API,以防止 Android 上出现堆内存不足的问题。我想通过将整个“标签”值数组作为 JSON 字符串读取来进一步加快处理速度。在此示例中,我想要一个 JSON 字符串,其中包含每个报告 ID 的所有三个标签及其值。
我的数据的简化版本是:
"TagRpts":[
{ "ID":46,
"Tags":[
{ "Name":"Tag1",
"Values":[
{"t":"Instance 1","v":"407"},
{"t":"Instance 2","v":"360"},
{"t":"Instance 3","v":"309"}]},
{ "Name":"Tag2",
"Values":[
{"t":"Instance 1","v":"926"},
{"t":"Instance 2","v":"168"},
{"t":"Instance 3","v":"366"}]},
{ "Name":"Tag3",
"Values":[
{"t":"Instance 1","v":"744"},
{"t":"Instance 2","v":"668"},
{"t":"Instance 3","v":"62"}]}]},
{ "ID":47,
"Tags":[
{ "Name":"machine 1",
"Values":[
{"t":"Instance 1","v":"613"},
{"t":"Instance 2","v":"882"},
"t":"Instance 3","v":"602"}]},
{ "Name":"machine 2",
"Values":[
{"t":"Instance 1","v":"38"},
{"t":"Instance 2","v":"38"},
{"t":"Instance 3","v":"329"}]},
{ "Name":"machine 3",
"Values":[
{"t":"Instance 1","v":"276"},
{"t":"Instance 2","v":"721"},
{"t":"Instance 3","v":"660"}]}]}
]
我目前正在使用此代码阅读它(为说明而简化 - 将处理逗号的区域删除到 stringbuilder 等):
if ("TagRpts".equals(fieldname)) { // TagRpts
jParser.nextToken(); // [ or null
if (jParser.getText() != "null") {
while (jParser.nextToken() != JsonToken.END_ARRAY) { // Tag report Array
while (jParser.nextToken() != JsonToken.END_OBJECT) {
fieldname = jParser.getCurrentName();
if ("ID".equals(fieldname)) {
jParser.nextToken();
aTagReport.ID = jParser.getText();}
if ("Tags".equals(fieldname)) {
StringBuilder sb = new StringBuilder();
sb.append("[");
while(jParser.nextToken() != JsonToken.END_ARRAY) {
fieldname = jParser.getCurrentName();
if ("Name".equals(fieldname)) {
jParser.nextToken();
sb.append("{\"Name\":\"" + Utility.encodeJSON(jParser.getText()) + "\"");}
if ("Values".equals(fieldname)) {
sb.append("\"Values\":[");
while(jParser.nextToken() != JsonToken.END_ARRAY) {
fieldname = jParser.getCurrentName();
if ("t".equals(fieldname)) {
jParser.nextToken();
sb.append("{\"t\":\"" + Utility.encodeJSON(jParser.getText()) + "\"");}
if ("v".equals(fieldname)) {
jParser.nextToken();
sb.append(",\"v\":\"" + Utility.encodeJSON(jParser.getText()) + "\"}");}
}
sb.append("]}");
}
}
sb.append("]");
aTagReport.Tags = sb.toString();
}
}
aTagReport.SaveTagReport();
aTagReport = new TagReport();
}
}
}