gpt4 book ai didi

Java JSON 到数组

转载 作者:行者123 更新时间:2023-11-29 04:51:03 25 4
gpt4 key购买 nike

我必须用 Java 解析 JSON,但我卡住了。

我的代码是这样的:

<%@ page import="javax.net.ssl.HttpsURLConnection"%>
<%@ page import="com.google.gdata.client.*"%>
<%@ page import="com.google.gdata.client.calendar.*"%>
<%@ page import="com.google.gdata.data.*"%>
<%@ page import="com.google.gdata.data.extensions.*"%>
<%@ page import="com.google.gdata.util.*"%>
<%@ page import="java.net.URL"%>
<%@ page import="java.io.BufferedReader"%>
<%@ page import="java.io.InputStreamReader"%>
<%@ page import="org.json.JSONArray"%>
<%@ page import="org.json.JSONObject"%>
<%@ page import="java.util.Arrays"%>
<%@ page import="java.util.ArrayList"%>

<%
String sb = "{\"photos\":{\"page\":1,\"pages\":1,\"perpage\":99,\"total\":\"6\",\"photo\":[{\"id\":\"23106093192\",\"owner\":\"130211131@N08\",\"secret\":\"5bf3d1b380\",\"server\":\"652\",\"farm\":1,\"title\":\"Student dorm at night\",\"ispublic\":1,\"isfriend\":0,\"isfamily\":0},{\"id\":\"22338004435\",\"owner\":\"123789722@N08\",\"secret\":\"e867cf3148\",\"server\":\"5815\",\"farm\":6,\"title\":\"Night lights\",\"ispublic\":1,\"isfriend\":0,\"isfamily\":0},{\"id\":\"16902142400\",\"owner\":\"123789722@N08\",\"secret\":\"ccaf7a0a08\",\"server\":\"7615\",\"farm\":8,\"title\":\"Rapa galbena\",\"ispublic\":1,\"isfriend\":0,\"isfamily\":0},{\"id\":\"16293296811\",\"owner\":\"123789722@N08\",\"secret\":\"1f524b67ca\",\"server\":\"7482\",\"farm\":8,\"title\":\"Night lights\",\"ispublic\":1,\"isfriend\":0,\"isfamily\":0},{\"id\":\"15828075371\",\"owner\":\"37402518@N06\",\"secret\":\"66474ae8a1\",\"server\":\"8396\",\"farm\":9,\"title\":\"Iasi at Night\",\"ispublic\":1,\"isfriend\":0,\"isfamily\":0},{\"id\":\"14026680657\",\"owner\":\"123789722@N08\",\"secret\":\"1ca0549a7b\",\"server\":\"2923\",\"farm\":3,\"title\":\"Iasi\",\"ispublic\":1,\"isfriend\":0,\"isfamily\":0}]},\"stat\":\"ok\"}";



JSONObject obj = new JSONObject(sb);
JSONObject photos = obj.getJSONObject("photos");

out.println(photos.get("photo"));
%>

输出:

[{"isfamily":0,"farm":1,"id":"23106093192","title":"Student dorm at night","ispublic":1,"owner":"130211131@N08","secret":"5bf3d1b380","server":"652","isfriend":0},{"isfamily":0,"farm":6,"id":"22338004435","title":"Night lights","ispublic":1,"owner":"123789722@N08","secret":"e867cf3148","server":"5815","isfriend":0},{"isfamily":0,"farm":8,"id":"16902142400","title":"Rapa galbena","ispublic":1,"owner":"123789722@N08","secret":"ccaf7a0a08","server":"7615","isfriend":0},{"isfamily":0,"farm":8,"id":"16293296811","title":"Night lights","ispublic":1,"owner":"123789722@N08","secret":"1f524b67ca","server":"7482","isfriend":0},{"isfamily":0,"farm":9,"id":"15828075371","title":"Iasi at Night","ispublic":1,"owner":"37402518@N06","secret":"66474ae8a1","server":"8396","isfriend":0},{"isfamily":0,"farm":3,"id":"14026680657","title":"Iasi","ispublic":1,"owner":"123789722@N08","secret":"1ca0549a7b","server":"2923","isfriend":0}]

我所能做的就是将所需的“数组”输出为字符串。

我需要将打印的字符串解析为数组数组。

试图从输出中做另一个 JSON 对象,但这个方法似乎失败了......

有什么建议吗?

最佳答案

您的输出中有对象数组 photos.get("photo")

您可以将其包装在 JSONArray

JSONArray arr = photos.getJSONArray("photo");
arr.getJSONObject(0).getString("title"); // or you can loop over all objects

关于Java JSON 到数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35394998/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com