简体   繁体   English

使用json-simple将多个JSON字符串解析为对象

[英]Parsing Multiple JSON Strings to Objects using json-simple

I have a socket server running, which will emit json strings for clients. 我有一个正在运行的套接字服务器,它将为客户端发出json字符串。 I tried to use json-simple for parsing them. 我试图使用json-simple进行解析。 But, the problem I face is that, the server doesn't have any delimiter character to segregate json strings. 但是,我面临的问题是,服务器没有任何分隔符来分隔json字符串。 So, my json-simple JSONParser throws ParseException. 因此,我的JSON简单JSONParser引发了ParseException。

As an alternate, I tried to use json-smart. 另外,我尝试使用json-smart。 But, in this case, the JSONParser returns only the first object and ignores rest of the string. 但是,在这种情况下,JSONParser仅返回第一个对象,而忽略字符串的其余部分。

I'm new to this json parsing stuff. 我是这个json解析新手。 It would be great if people can direct me to correct way of handling json string streams. 如果人们可以指导我采取正确的方式处理json字符串流,那就太好了。

Edit: - Adding JSON String and Sample Code 编辑:-添加JSON字符串和示例代码

{"type":"response","id":"1","result":[true,0]}{"type":"response","id":"2","result":[true,1]}

Currently this method returns the single object when I use json-smart and null when json-simple is used. 当前,当我使用json-smart时,此方法返回单个对象;使用json-simple时,此方法返回null。

public JSONObject getResponse(JSONObject request) {
    String s = null;
    Socket soc = null;
    PrintWriter sout = null;
    BufferedReader sin = null;
    try {
        soc = new Socket(InetAddress.getByName("127.0.0.1"), 9090);
        sout = new PrintWriter(soc.getOutputStream());
        sin = new BufferedReader(
                new InputStreamReader(soc.getInputStream()));
        sout.println(request.toJSONString());
        sout.flush();
        s = sin.readLine();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    } finally {
        try {
            sin.close();
            sout.close();
            soc.close();
        } catch (Exception e) {
        }
    }
    Object response = null;
    try {
        response = JSONValue.parseWithException(s.toString());
    }catch (ParseException e){
        e.printStackTrace();
    }
    return (JSONObject) response;

Thanks in advance, 提前致谢,

Kaja 卡哈

I have found a solution using Jackson . 我找到了使用Jackson的解决方案。 Here is the code that worked for me. 这是对我有用的代码。

MappingJsonFactory factory = new MappingJsonFactory();
JsonParser parser = factory.createParser(soc.getInputStream());
JsonToken curToken = parser.nextToken();
if (curToken != JsonToken.START_OBJECT) {
    System.err.println("Not in start object!, Exiting...");
    return null;
}
while (runParser.get() == true) {
    if (curToken == JsonToken.START_OBJECT) {
        TreeNode node = parser.readValueAsTree();
        System.out.println(node.getClass().getName());
        System.out.println(node);
    }
    curToken = parser.nextToken();
}

Thanks Kaja Mohideen! 感谢Kaja Mohideen! It's working with a small change in while loop. 它在while循环中进行了很小的更改。 This code works perfectly. 此代码完美地工作。 In my case input json is in file. 在我的情况下,输入json在文件中。 libs used : jackson-core, jackson-annotation and jackson-databind 使用的库:杰克逊核心,杰克逊注释和杰克逊数据绑定

    MappingJsonFactory factory = new MappingJsonFactory();
    JsonParser parser = null;
    File file = new File("jsontest.txt");
    try {

        parser = factory.createParser(file);
        JsonToken curToken = parser.nextToken();
        if (curToken != JsonToken.START_OBJECT) {
            System.err.println("Not in start object!, Exiting...");

        }
        while (parser.hasCurrentToken()) {
            if (curToken == JsonToken.START_OBJECT) {
                TreeNode node = parser.readValueAsTree();
                System.out.println(node);
            }
            curToken = parser.nextToken();
        }
    } catch (JsonParseException e) {
        e.printStackTrace();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM