JAVA JSONObject, JSONArray (org.json, org.json.simple)
org.json.JSONObject
import org.json.JSONObject
String jsonString = "{\"test\": \"testtest\","\"test2\": \"testtesttest\"}";
JSONObject jObject = new JSONObject(jsonString);
String test = jObject.getString("test");
org.json.JSONObject, org.json.JSONArray 조합
import org.json.JSONArray;
import org.json.JSONObject;
String jsonString = "{"
+ "\"list\": ["
+ "{"
+ "\"title\": \"title1\","
+ "\"contents\": \"contents1""
+ "},"
+ "{"
+ "\"title\": \"title2\","
+ "\"contents\": \"contents1\""
+ "},"
+ "{"
+ "\"title\": \"title3\","
+ "\"contents\": \"contents1\""
+ "}"
+ "]"
+"}";
// JSONObject 구조로된 String 변수를 JSONObject로 변환을 합니다.
JSONObject jObject = new JSONObject(jsonString);
// JSON 구조에 있는 배열을 가져옵니다.
JSONArray jArray = jObject.getJSONArray("list");
// 배열의 모든 아이템을 출력합니다.
for (int i = 0; i < jArray.length(); i++) {
JSONObject obj = jArray.getJSONObject(i);
String title = obj.getString("title");
String contents = obj.getString("contents");
System.out.println("title " + i + " : " + title);
System.out.println("contents " + i + " + contents);
System.out.println();
}
org.json.simple.JSONObject, org.json.simple.JSONArray
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
String jsonString = "{"
+ "\"list\": ["
+ "{"
+ "\"title\": \"title1\","
+ "\"contents\": \"contents1""
+ "},"
+ "{"
+ "\"title\": \"title2\","
+ "\"contents\": \"contents1\""
+ "},"
+ "{"
+ "\"title\": \"title3\","
+ "\"contents\": \"contents1\""
+ "}"
+ "]"
+"}";
JSONObject jsonObject = new JSONObject();
jsonObject = (JSONObject)parser.parse(jsonString);
JSONArray jsonArray = jsonObject.getJSONArray("list");
for (Object value : jsonArray) {
JSONObject v = (JSONObject) value;
system.out.println("value : " + v.get("test").toString());
}
최신 댓글