Skip to content

How to read multiple JSON syntax blocks in a single input #269

@ssfang

Description

@ssfang

See also Read Multiple Objects JSON with Java.

If a JSON object is parsed, it is expected to be able to get the current position like

Error example,

package ss;

import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;
import net.minidev.json.JSONValue;
import net.minidev.json.parser.JSONParser;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;

import static net.minidev.json.parser.JSONParser.DEFAULT_PERMISSIVE_MODE;

public class JsonSmartExample {
    public static void main(String[] args) throws Exception {
        testMultipleJsons2();
        String json = "[{\"friends\":[{\"id\":0,\"name\":\"test1\"},{\"id\":1,\"name\":\"test2\"}]}] other data";
        JSONArray root = (JSONArray) JSONValue.parseWithException(json);
        JSONObject rootObj = (JSONObject) root.get(0);
        JSONArray array = (JSONArray) rootObj.get("friends");
        for (int idx = 0; idx < array.size(); idx++) {
            JSONObject cap = (JSONObject) array.get(idx);
            String first = (String) cap.get("name");
            System.out.println(first);
        }
    }

    private static void testMultipleJsons() throws Exception {
        String json = "{tranid:\"1212\", \"user\":{\"name\":\"123\",\"addr\":\"786 rt\"}}"
                + "\n{tranid:\"1213\", \"user\":{\"name\":\"345\",\"addr\":\"4234 iu\"}}";
        ByteArrayInputStream stream = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8));
        JSONObject root1 = (JSONObject) JSONValue.parseWithException(stream);
        System.out.println(root1.get("tranid"));
        System.out.println(stream.available());
        JSONObject root2 = (JSONObject) JSONValue.parseWithException(stream);
        System.out.println(root2.get("tranid"));
    }

    private static void testMultipleJsons2() throws Exception {
        String json = "{tranid:\"1212\", \"user\":{\"name\":\"123\",\"addr\":\"786 rt\"}}"
                + "\n{tranid:\"1213\", \"user\":{\"name\":\"345\",\"addr\":\"4234 iu\"}}";
        JSONParser parser = new JSONParser(DEFAULT_PERMISSIVE_MODE);
        JSONObject root1 = (JSONObject) parser.parse(json, JSONValue.defaultReader.DEFAULT);
        System.out.println(root1.get("tranid"));
        // for (Class<?> c = parser.getClass(); c != null; c = c.getSuperclass()) Field f = c.getDeclaredField("pos");
        Field field = Class.forName("net.minidev.json.parser.JSONParserBase").getDeclaredField("pos");
        field.setAccessible(true);
        Field pStringField = JSONParser.class.getDeclaredField("pString");
        pStringField.setAccessible(true);
        Object parser2 = pStringField.get(parser);
        Integer ipos = (Integer) field.get(parser2);
        System.out.println("pos=" + ipos);
        JSONObject root2 = (JSONObject) parser.parse(json.substring(ipos), JSONValue.defaultReader.DEFAULT);
        System.out.println(root2.get("tranid"));
        ipos = (Integer) field.get(parser2);
        System.out.println("pos=" + ipos);
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions