“ org.json.simple.JSONArray无法转换为org.json.simple.JSONObject”

问题描述 投票:0回答:1

我正在尝试解析文件,以便将JSON数据发布到需要访问令牌的本地平台中。但我收到此错误:org.json.simple.JSONArray无法转换为org.json.simple.JSONObject我已经尝试了多种方法来修复它,但是我没有用。

这是代码:这是我用来将csv文件转换为Json格式的一部分

import java.io.File;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
import java.io.DataInputStream;
import java.io.File;
//import org.json.JSONObject;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.json.simple.JSONArray;

public class CSVtoJSON {
     public static void main(String[] args) throws Exception {
            File input = new File("ArgosData_2020_05_24_22_21_59.csv");
            File output = new File("output.json");
            String access_token = "rxmqDZF0J1BCUIDm1Faw";

            CsvSchema csvSchema = CsvSchema.builder().setUseHeader(true).build();
            CsvMapper csvMapper = new CsvMapper();

            // Read data from CSV file
            List<Object> readAll = csvMapper.readerFor(Map.class).with(csvSchema).readValues(input).readAll();

            ObjectMapper mapper = new ObjectMapper();

            // Write JSON formated data to output.json file
            mapper.writerWithDefaultPrettyPrinter().writeValue(output, readAll);

            // Write JSON formated data to stdout
            System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(readAll));

然后我尝试发布数据,在这一部分中,我收到了错误消息:

 try {
                JSONParser parser = new JSONParser();
                //Use JSONObject for simple JSON and JSONArray for array of JSON.
                JSONObject data = (JSONObject) parser.parse(
                        new FileReader(output.getAbsolutePath()));//path to the JSON file.
                System.out.println(data.toJSONString());

                String paramValue = "param\\with\\backslash";
                String yourURLStr = "http://host.com?param=" + java.net.URLEncoder.encode(paramValue, "UTF-8");

                URL url2 = new URL("http://localhost:8080");
                HttpsURLConnection conn = (HttpsURLConnection) url2.openConnection();
                conn.setRequestMethod("POST");
                conn.setRequestProperty("Content-Type", "application/json");
                conn.setRequestProperty("Accept", "application/json");
                conn.setRequestProperty("Authorization", "Bearer" + access_token);

                conn.setDoOutput(true);
                OutputStream outStream = conn.getOutputStream();
                OutputStreamWriter outStreamWriter = new OutputStreamWriter(outStream, "UTF-8");
                outStreamWriter.write(data.toJSONString());
                outStreamWriter.flush();
                outStreamWriter.close();
                outStream.close();
                String response = null;

                System.out.println(conn.getResponseCode());
                System.out.println(conn.getResponseMessage());

                DataInputStream input1 = null;
                input1 = new DataInputStream (conn.getInputStream());
                while (null != ((response = input1.readLine()))) {
                    System.out.println(response);
                    input1.close ();
                }
            } catch (IOException | ParseException e) {
                e.printStackTrace();
            }
        }

}



[如果有人知道如何执行此操作,那么我是编程新手,不胜感激能得到一些帮助。

java json parsing post
1个回答
0
投票

您正在编写对象列表,并尝试读取单个对象。

如果您查看文件,则可能会看到类似以下内容的内容:[{"name": "Object1", value="1"}, {"name": "Object2"}, {"name": Object3"}]

使用JSONObject data = (JSONObject) parser.parse(...,您正在尝试读取单个JSON对象,例如{"name":"i'm a single object' "value"="42"}。要解决迫在眉睫的问题,请使用JSONArray data代替消息提示的JSONObject

我不熟悉org.json.simple(是gson吗?),但是也许有一种方法可以不使用JSONArray而直接将其反序列化为List。快速搜索显示this question

© www.soinside.com 2019 - 2024. All rights reserved.