简体   繁体   中英

Parse a file of JSON objects and convert to JSON array

I have a file that contains JSON objects. I am trying to access the file using its URL in my java program to access its contents as follows.

URL url = new URL(Url);
URLConnection request = url.openConnection();
request.connect();
JsonReader jr = new JsonReader(new InputStreamReader((InputStream) request.getContent()));
jr.setLenient(true);
JsonParser jp = new JsonParser();
JsonElement root = jp.parse(jr);

I have to use the jr.setLenient(true); since the data is not a JSON object, but multiple JSON objects. But this approach does not help me to get all the JSON objects in the file. I am trying to get all the JSON objects, loop through each one of them and then save the data into my database.

I have the sample of the type of data I receive at the following URL: Sample File

Is there any way in which I can get all the objects in the file to perform the required operations.

Also, I can manually save the data into a JSON array in a new file manually if nothing works, but there is no comma separation between each JSON object which is making it very difficult for me to parse the data.

But each JSON object is a new line. So is there any way to parse it based on that.

what @marekful suggest was to fix the JSON string to get a valid one

public static void main(String[] args) throws Exception {
    URL url = new URL("http://d1kv7s9g8y3npv.cloudfront.net/testsite/files/doc-lib/2018/05/15/10/21/48/448/head/test3.txt#");
    URLConnection request = url.openConnection();
    request.connect();
    final Object content = request.getContent();

    BufferedReader br = new BufferedReader(new InputStreamReader((InputStream) content));
    String inputLine;

    List<String> jsonEntries = new ArrayList<>();
    while ((inputLine = br.readLine()) != null) {
        jsonEntries.add(inputLine);
    }
    String jsonString = "["  + jsonEntries.stream().collect(Collectors.joining(",")) +"]";

    JsonParser jp = new JsonParser();
    JsonElement root = jp.parse(jsonString);
    System.out.println(root);
}

}

I found a solution which worked well for me

URL url = new URL(urlString.trim());
URLConnection request = url.openConnection();
request.connect();
BufferedReader in = new BufferedReader(
        new InputStreamReader(
                request.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) 
{
    jsonObject = new JSONObject(inputLine);
    jsonArray.put(jsonObject);
}
in.close();

I was able to create a JSON array using the above approach and then iterate over that array to save the data as per my requirement

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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