读多条json线

时间:2017-04-11 14:46:15

标签: java json rest jackson

我从网址获得以下json:

{"bytes": 5043, "name": "a", "timestamp": "11-Apr-2017 12:11:51", "ds": "Lab1"}
{"bytes": 0, "name": "b", "timestamp": "11-Apr-2017 12:11:51", "ds": "Lab1"}
{"bytes": 11590, "name": "c", "timestamp": "11-Apr-2017 12:11:51", "ds": "Lab1"}

我想返回每行的字节数。使用Jackson我尝试解析它如下(注意:json保存到文件中进行测试):

package JsonRead;

import java.io.File;
import java.io.IOException;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JacksonTreeModelLogStorage {
    public static void main(String[] args) {

        try{

            ObjectMapper mapper = new ObjectMapper();

            JsonNode root = mapper.readTree(new File("JsonRead/json2.json"));

            JsonNode logStorageNode = root;

            for(JsonNode node : logStorageNode){
                String bytesUsed = node.path("bytes").asText();
                System.out.println("Bytes Used: " + bytesUsed);
            }

        } catch(IOException e){
                System.out.println("IO Exception " + e );
        }
    }
}

返回以下内容:

Bytes Used: 
Bytes Used: 
Bytes Used: 
Bytes Used: 

如果我将for循环更改为以下内容,我可以看到我返回json的第一行:

for(JsonNode node : logStorageNode){
            String json = node.asText();
            System.out.println("json: " + a);
}

如何实现json所有行的字节?

2 个答案:

答案 0 :(得分:2)

您发布的内容无效JSON:JSON规范不允许并排显示多个对象。

如果您遇到此类文本(不能将其视为JSON),您需要将其拆分为分隔符,以便每个子字符串都是您能够解析的有效JSON对象。
在您的情况下,似乎换行是一个有效的分隔符,但您应该确保您的JSON对象永远不会包含任何换行符。如果是这种情况,您可以按如下方式解析文本:

try(
    FileReader reader = new FileReader("JsonRead/json2.json");
    BufferedReader bufferedReader = new BufferedReader(reader);
) {
    String currentLine;
    while((currentLine=bufferedReader.readLine()) != null) {
        JsonNode logStorageNode = mapper.readTree(currentLine);
        // [...] carry on with your current code.
    }
}

我首先假设你收到了一个数组:

[
    {"bytes": 5043, "name": "a", "timestamp": "11-Apr-2017 12:11:51", "ds": "Lab1"},
    {"bytes": 0, "name": "b", "timestamp": "11-Apr-2017 12:11:51", "ds": "Lab1"},
    {"bytes": 11590, "name": "c", "timestamp": "11-Apr-2017 12:11:51", "ds": "Lab1"}
]

在这种情况下,您应该循环其内容:

JsonNode root = mapper.readTree(new File("JsonRead/json2.json"));

if (!root.isArray()) {
    throw WhateverException("A JSON Array is expected");
}

// a cast to ArrayNode would be possible, but isn't necessary
// as per the design of jackson's JsonNode
Iterator<JsonNode> it = root.iterator();

while (it.hasNext()) {
    JsonNode logStorageNode = it.next();

    // [...] carry on with your current code.
}

答案 1 :(得分:0)

谢谢你们。这是一个很大的帮助。

我根据自己的需要编辑了一下这个尝试。这就是我最终得到的结果:

ObjectMapper mapper = new ObjectMapper();
    try(
            FileReader reader = new FileReader("JsonRead/json2.json");
            BufferedReader bufferedReader = new BufferedReader(reader);
        ) {
            String currentLine;
            while((currentLine=bufferedReader.readLine()) != null) {
                Vault vlt = mapper.readValue(currentLine, Vault.class);
                System.out.println(vlt.getBytes());
            } 
        }