简体   繁体   English

解析JSON对象文件并转换为JSON数组

[英]Parse a file of JSON objects and convert to JSON array

I have a file that contains JSON objects. 我有一个包含JSON对象的文件。 I am trying to access the file using its URL in my java program to access its contents as follows. 我正在尝试在Java程序中使用其URL访问文件,以按如下方式访问其内容。

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); 我必须使用jr.setLenient(true); since the data is not a JSON object, but multiple JSON objects. 因为数据不是JSON对象,而是多个JSON对象。 But this approach does not help me to get all the JSON objects in the file. 但是这种方法并不能帮助我获取文件中的所有JSON对象。 I am trying to get all the JSON objects, loop through each one of them and then save the data into my database. 我正在尝试获取所有JSON对象,遍历每个对象,然后将数据保存到我的数据库中。

I have the sample of the type of data I receive at the following URL: Sample File 我有以下URL接收到的数据类型的样本: 样本文件

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. 另外,如果没有任何效果,我可以手动将数据手动保存到新文件中的JSON数组中,但是每个JSON对象之间没有逗号分隔,这使我很难解析数据。

But each JSON object is a new line. 但是每个JSON对象都是换行符。 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 @marekful的建议是修复JSON字符串以获得有效的字符串

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 我能够使用上述方法创建JSON数组,然后对该数组进行迭代以根据我的要求保存数据

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

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