1

I have to parse a JSON in Java, but I got stuck.

My code looks like this:

<%@ 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"));
%>

Output:

[{"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}]

All I could do is to output the desired "array" as a string.

I need to parse the printed string to an array of arrays.

Tried to do another JSON object from the output, but this method seems to fail...

Any sugestions?

2 Answers 2

3

You have array of objects in your output photos.get("photo")

You can wrap that in JSONArray

JSONArray arr = photos.getJSONArray("photo");
arr.getJSONObject(0).getString("title"); // or you can loop over all objects
Sign up to request clarification or add additional context in comments.

1 Comment

Tried your idea, but i get the following error: The type of the expression must be an array type but it resolved to JSONArray
0

Solved:

JSONArray arr = photos.getJSONArray("photo");
//arr[0].getString('title'); // or you can loop over all objects // title should have double quotes

JSONObject item   = arr.getJSONObject(0);
    String result = item.getString("title");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.