Java 中 put() 方法在将数据放入 JsonObject 时出错。

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

我对我的错误代码有疑问。我正在开发一个使用 Angular、Java、Spark 和 SQL 数据库的项目。

该方法包含一个带有 SQL Select 语句的准备语句。在while循环中只要找到结果就想遍历一遍。将查询到的数据打包成一个Json对象,然后将这些Json对象打包成一个Json数组。

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import finanzplanpackage.connection.ConnectionConfiguration;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.lang.String;

import java.util.logging.Level;
import java.util.logging.Logger;



public class n_paresmodul {

    private ConnectionConfiguration c = new ConnectionConfiguration();

    public String viewdata() {
        String data = "";
        JsonArray ja = new JsonArray();
        try{
        PreparedStatement pre = c.getconn().prepareStatement("SELECT * FROM n_pares");
        ResultSet res = pre.executeQuery();
        while (res.next()) {
            JsonObject jo = new JsonObject();
            jo.put("account_id", res.getInt("account_id"));
            jo.put("mandant_id", res.getInt("mandant_id"));
            jo.put("id", res.getInt("id"));
            jo.put("sourcedata", res.getString("sourcedata"));
            ja.add(jo);
        }
        data = ja.toString();
    } catch(
    SQLException ex)

    {
        Logger.getLogger(n_paresmodul.class.getName()).log(Level.SEVERE, null, ex);
    }

    return data;

}

编译时,编译器向我抛出以下错误消息。

符号:方法put(java.lang.String, int) location:com 类型的变量 jo。谷歌。 gson.JsonObject

enter image description here

有人可以帮助我并告诉我 put 方法做错了什么吗?

谢谢你

java json methods
2个回答
1
投票

com.google.gson.JsonObject
没有任何方法
put()

您应该尝试使用
addProperty("account_id", res.getInt("account_id"))


0
投票

它是大写的

JSONObject
。我也犯过这个错误。

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