【问题标题】:How to convert InputStream to JsonArray Object using Java如何使用 Java 将 InputStream 转换为 JsonArray 对象
【发布时间】:2022-01-12 17:40:27
【问题描述】:

我正在尝试将InputStream 转换为JSON Array object,但没有正确获取JSON 对象,请在下面找到我的inputStream 记录:

{"id":4,"productId":9949940,"data":"product data 1","productPrice":"653.90"}
{"id":5,"productId":4940404,"data":"product data 2","productPrice":"94.12"}

我为每个项目和最后一条记录添加了额外的逗号 - 请在下面找到 java 代码。有人可以帮我解决这个问题。提前感谢您的帮助。谢谢!

Product.java

public void getProduct() {

    String bucketName = "myProductBucket";
    String key = "products/product-file";
            StringBuilder sb = null;
            JSONArray jsonArray = new JSONArray();
            try(InputStream inputStream = s3Service.getObjectFromS3(bucketName, key);) {
                sb = new StringBuilder();
                sb.append("[");
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
                String line;
                while((line = reader.readLine()) != null) {
                    sb.append(line).append(",");
                }
                sb.append("]");

            } catch (IOException e) {
                e.printStackTrace();
            }

            System.out.println(sb.toString());
}

输出:

[{"id":4,"productId":9949940,"data":"product data 1","productPrice":"653.90"},,
{"id":5,"productId":4940404,"data":"product data 2","productPrice":"94.12"},]

预期输出:

[{"id":4,"productId":9949940,"data":"product data 1","productPrice":"653.90"},
{"id":5,"productId":4940404,"data":"product data 2","productPrice":"94.12"}]

【问题讨论】:

  • 尝试使用BufferedReaderInputStream中的字符串读出为StringBuilder,您可以将其转换为JsonObjectconvert an InputStream to a JsonObject
  • 整体而言,您的输入流内容并不代表有效的 JSON 内容。每个单独的 JSON 对象都是有效的,这就是 JSONObject 所消耗的,第一行/对象。
  • 最后一个逗号出现,因为你无条件附加它,尝试相反:在下一个对象之前添加逗号如果缓冲区不为空。由于空行,对象之间的逗号可能会出现:在将 line 提供给构建器之前对其进行测试。或者,您可以手动创建JSONArray,然后用JSONObjects 逐一填充它,这(可以说)更容易,客观上更有效。

标签: java json inputstream


【解决方案1】:

AFAIU,这是意料之中的,因为您的 JSON 对象只是部分有效。

虽然它也不是一个有效的 JSON 数组,但可以在稍作修改后解析为 JSONArray(注意开始和结束括号以及对象之间的逗号):

[
{"id":4,"productId":9949940,"data":"product data 1","productPrice":"653.90"},
{"id":5,"productId":4940404,"data":"product data 2","productPrice":"94.12"}
]

或者,您也可以手动将输入拆分为单独的 JSON 对象并逐个解析。

【讨论】:

    【解决方案2】:

    根据您的产品详细信息文件,您的文件中没有有效的 JSON 数组对象。因此,无法直接从文件中创建 JSONArray。

    你可以做的是,一一阅读产品线并创建JSONObject并将其转换为JSONArray。请在下面找到可以提供帮助的代码。

    Scanner s = new Scanner(new File("filepath")); //Read the file
    JSONArray jsonArray = new JSONArray();
    
    while (s.hasNext()){
       JSONObject jsonObject = new JSONObject(s.next());
       jsonArray.put(jsonObject);
    }
    //jsonArray can be print by iterating through it.
    

    【讨论】:

      【解决方案3】:

      这是您可以使用的代码,其想法是 InputStream 不代表有效的 JSON,因此您必须使用 StringBuilder 将其转换为有效的 JSON 字符串。但首先,您需要处理无效的 JSON。

              StringBuilder sb;
              try(InputStream inputStream = new FileInputStream(new File("Path"))) {
                  sb = new StringBuilder();
                  BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
      
                  String line;
                  while((line = reader.readLine()) != null) {
                      sb.append(line);
                  }
              } catch (IOException e) {
                  e.printStackTrace();
              }
              
              JSONArray jsonArray = new JSONArray(sb.toString());
      

      依赖性

      <dependency>
          <groupId>org.json</groupId>
          <artifactId>json</artifactId>
          <version>20211205</version>
      </dependency>
      
      

      【讨论】:

        猜你喜欢
        • 2015-08-24
        • 1970-01-01
        • 2017-10-20
        • 2021-07-06
        • 1970-01-01
        • 2021-06-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多