正如标题所示,我的字符串数组(List)的JSON格式存在问题
基本上,这是我希望看到的格式:
{"Item":
["Id: 1","Title: Surface Pro 4","Phone: 4077603835","Email:
XXXXXXXX@gmail.com","Description: Surface Pro 4 for sale, in
excellent condition, basically brand new!"],
"Item":
"Id: 2","Title: Macbook Air","Phone: null","Email:
null","Description: null"],
"Item":
"Id: 3","Title: Lenovo Laptop","Phone: 3433215565","Email:
XXXXXXXX.com","Description: Free Macbook Air, I promise"]
}
但是,这是我的代码生成的以下格式:
{"Item":["Id: 1","Title: Surface Pro 4","Phone: 4077603835","Email:
XXXXXXXX@gmail.com","Description: Surface Pro 4 for sale, in
excellent condition, basically brand new!","Id: 2","Title: Macbook
Air","Phone: null","Email: null","Description: null","Id: 3","Title:
Lenovo Laptop","Phone: 3433215565","Email:
XXXXXXXX.com","Description: Free Macbook Air, I promise"]}
我只想看到每个项目的单独数组!我认为问题在于这部分代码:" obj.put(" Item",items);"因为它将所有项目放在同一个数组中,但是如何动态创建不同的数组呢?我的项目清单将继续扩大,永远不会保持不变。
代码:
public List<String[]> SelectAllSQL() {
List<String[]> result = new ArrayList<String[]>();
sql = "select * from item";
try {
ResultSet rs = stmt.executeQuery(sql);
rs.last();
int lastRow = rs.getRow();
for(int row = 1; row <= lastRow; row++) {
rs.absolute(row);
result.add(new String[] {rs.getString("title"),
rs.getString("phone"), rs.getString("email"),
rs.getString("description"),
rs.getString("id")});
}
return result;
}
catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public String getIt() {
ConnectionDB db = new ConnectionDB();
QueryDB query = new QueryDB();
List<String[]> sqlResult = new ArrayList<String[]>();
db.Connect();
sqlResult = query.SelectAllSQL();
JSONObject obj = new JSONObject();
JSONArray items = new JSONArray();
for(String[] arr : sqlResult) {
items.add("Id: " + arr[4]);
items.add("Title: " + arr[0]);
items.add("Phone: " + arr[1]);
items.add("Email: " + arr[2]);
items.add("Description: " + arr[3]);
obj.put("Item", items);
}
return obj.toJSONString();
答案 0 :(得分:2)
我认为你正确的代码行创造了问题。您可以想到包含Key和Value对的JSON对象。在你的情况下,它的&#34;项目&#34;是键,数组是值。您可以通过其密钥访问JSON对象值,因此您不能使用相同的密钥对密钥和值组进行访问。
解决方案:让您的密钥与众不同。 &#34; Item1&#34;,&#34; Item2&#34;,&#34; Item3&#34;。还放置JSONArray items = new JSONArray();在您的范围内,如David所说
答案 1 :(得分:1)
两件事:
为循环内的每个项重新初始化JSONArray,以便sqlResult的每一行都有自己的属性数组。现有代码初始化它一次,这就是当前输出中的数组最终得到来自其中三个数据的数据。
for(String[] arr : sqlResult) {
JSONArray items = new JSONArray();
JSONObject不允许使用相同键的多个条目。 JSONObject将不允许您有三个条目,所有条目都具有相同的&#34;项目&#34;。当第二次和第三次调用obj.put("Item", items);
时,它将替换先前的&#34;项目&#34;而不是创建新的条目。许多可能的解决方案中有两个都需要更改所需的输出
答案 2 :(得分:1)
def.par <- par(no.readonly = TRUE) # save default, for resetting...
x <- pmin(3, pmax(-3, rnorm(5000)))
y <- pmin(3, pmax(-3, rnorm(5000)))
xhist <- hist(x, breaks=1000, plot=FALSE)
yhist <- hist(y, breaks=1000, plot=FALSE)
cut1 <- cut(xhist$density, c(-Inf,-1.8,1.8,Inf))
cut2 <- cut(yhist$density, c(-Inf,-2.9,2.9,Inf))
top <- max(c(xhist$density, yhist$density))
xrange <- c(-3,3)
yrange <- c(-3,3)
nf <- layout(matrix(c(2,0,1,3),2,2,byrow=TRUE), c(3,1), c(1,3), TRUE)#layout.show(nf)ZSSS
par(mar=c(3,3,0,0))
plot(x, y, xlim=xrange, ylim=yrange, type="p",pch=".",cex=1,xlab="T1", ylab="T2",col="grey")
abline(h=0.5,lty=2,lwd=1.5,col="black")
abline(v=-2.5,lty=2,lwd=1.5,col="black")
abline(v=2.5,lty=2,lwd=1.5,col="black")
par(mar=c(0,3,1,1))
breaks <- c(-Inf, -2.5, 2.5, Inf)
col1 <- c("blue", "grey", "red")[findInterval(xhist$breaks, vec=breaks)]
barplot(xhist$density, axes=TRUE,ylab="T3",border=col1)
par(mar=c(3,0,1,1))
col2 <- c("grey", "red")[(yhist$breaks >= 0.5) + 1]
barplot(yhist$density, axes=TRUE, xlab="T4",space=0, horiz=TRUE,border=col2)
将它保留在for循环中,因为它将重新创建JSONArray,否则会发生的事情是将其添加到与elmenets相同的数组中,因此将其保留在循环中