我正在尝试实现一个 Web 服务,该服务返回一个简单的数据库以从 Java 应用程序中读取它。
我的服务器正在运行 Tomcat 7。
这是应该创建我的 JSON 文件的代码:
<%@ page pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@page import="org.json.simple.JSONObject"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/png" href="HilevenLogo.png" />
<title>Hileven</title>
</head>
<body>
<%
Class.forName ("org.postgresql.Driver");
Connection cnx = DriverManager.getConnection ("jdbc:postgresql://localhost", "user" , "password");
Statement st = cnx.createStatement();
ResultSet rs = st.executeQuery("SELECT id, description, libelle, ordre, warning FROM commandes ORDER BY ordre");
JSONObject obj=new JSONObject();
while(rs.next()){
obj.put("id", new Integer(rs.getInt("id")));
obj.put("description", rs.getString("description"));
obj.put("libelle", rs.getString("libelle"));
obj.put("ordre", new Integer(rs.getInt("ordre")));
obj.put("warning", new Boolean(rs.getBoolean("warning")));
out.print(obj);
out.flush();
}
rs.close();
cnx.close();
%>
</body>
</html>
我的第一个问题是关于循环的。一个 JSON 文件是否可以有多个记录?因为我的代码显示了一个网页,例如:
{"ordre":1,"libelle":"Rapport Quotidien","description":"Envoie un rapport journalier de l activité du serveur par mail","warning":false,"id":1}{"ordre":2,"libelle":"Rapport Hebdomadaire","description":"Envoie un rapport hebdomadaire de l activité du serveur par mail","warning":false,"id":2}{"ordre":3,"libelle":"Reboot","description":"Redémarre le serveur","warning":false,"id":3}{"ordre":100,"libelle":"Extinction","description":"Eteint le serveur","warning":true,"id":4}
有 4 个 {}。这可能吗?
然后我也在这个论坛上上了阅读课:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.nio.charset.Charset;
import org.json.JSONException;
import org.json.JSONObject;
public class JsonReader {
private static String readAll(Reader rd) throws IOException {
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
return sb.toString();
}
public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
InputStream is = new URL(url).openStream();
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonText = readAll(rd);
System.out.println(jsonText);
JSONObject json = new JSONObject(jsonText);
return json;
} finally {
is.close();
}
}
public static void main(String[] args) throws IOException, JSONException {
JSONObject json = readJsonFromUrl("http://www.hileven.com/admin");
System.out.println(json.toString());
System.out.println(json.get("id"));
}
}
我有这个错误:
Exception in thread "main" org.json.JSONException: A JSONObject text must begin with '{' at character 6
在行:
JSONObject json = new JSONObject(jsonText);
所以我在这一行之前添加了一个 System.out.println(jsonText) ,它返回我页面的整个 html 代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/png" href="HilevenLogo.png" />
<title>Hileven</title>
</head>
<body>
{"ordre":1,"libelle":"Rapport Quotidien","description":"Envoie un rapport journalier de l activité du serveur par mail","warning":false,"id":1}{"ordre":2,"libelle":"Rapport Hebdomadaire","description":"Envoie un rapport hebdomadaire de l activité du serveur par mail","warning":false,"id":2}{"ordre":3,"libelle":"Reboot","description":"Redémarre le serveur","warning":false,"id":3}{"ordre":100,"libelle":"Extinction","description":"Eteint le serveur","warning":true,"id":4}
</body>
</html>
正常吗?我认为我的 JSON Web 服务配置不当。当我们尝试访问 URL 而不是打印所有 JSON 内容时,不是必须开始下载 JSON 文件吗?
如何使我的 JSP 仅返回 JSON 部分,甚至如何使我的 java 程序仅读取 JSON 部分而不是所有 html 代码?
最佳答案
问题出在 JSON 结构上,因为它的结构无效。您需要用逗号 ,
分隔每条记录,并将整个 JSON 括在 []
内,因为您有符号列表。
[{"ordre":1,"libelle":"Rapport Quotidien","description":"Envoie un rapport journalier de l activité du serveur par mail","warning":false,"id":1},{"ordre":2,"libelle":"Rapport Hebdomadaire","description":"Envoie un rapport hebdomadaire de l activité du serveur par mail","warning":false,"id":2},{"ordre":3,"libelle":"Reboot","description":"Redémarre le serveur","warning":false,"id":3},{"ordre":100,"libelle":"Extinction","description":"Eteint le serveur","warning":true,"id":4}]
您需要 JSON 数组和对象(每行)。
JSONObject obj;
JSONArray list = new JSONArray();
while(rs.next()){
obj=new JSONObject();
list.add(obj);
obj.put("id", new Integer(rs.getInt("id")));
obj.put("description", rs.getString("description"));
obj.put("libelle", rs.getString("libelle"));
obj.put("ordre", new Integer(rs.getInt("ordre")));
obj.put("warning", new Boolean(rs.getBoolean("warning")));
}
out.print(list);
out.flush();
同时更改此行。
JSONObject json = new JSONObject(jsonText);
至
JSONArray json = new JSONArray(jsonText);
关于java - java程序的JSON问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40889684/