JSON 자바스크립트 개체 표기법을 나타냅니다. 전송 하는 데 사용할 수 있습니다. 및 저장소 데이터.
JSON객체 문자열에서 텍스트를 구문 분석하여 지도와 유사한 개체를 생성할 수 있습니다. . 개체는 내용을 조작하고 JSON 호환 개체 직렬화를 생성하기 위한 메서드를 제공합니다. JSONArray 문자열에서 텍스트를 구문 분석하여 벡터와 유사한 개체를 생성할 수 있습니다. . 개체는 내용을 조작하고 JSON 호환 배열 직렬화를 생성하기 위한 메서드를 제공합니다.
아래 두 가지 예에서 JSON 문자열을 JSON 개체로 변환할 수 있습니다. .
예시 1
import org.json.JSONObject;
import org.json.JSONArray;
public class StringToJSONTest {
public static void main(String args[]) {
String str = "[{\"No\":\"1\",\"Name\":\"Adithya\"},{\"No\":\"2\",\"Name\":\"Jai\"}, {\"No\":\"3\",\"Name\":\"Raja\"}]";
JSONArray array = new JSONArray(str);
for(int i=0; i < array.length(); i++) {
JSONObject object = array.getJSONObject(i);
System.out.println(object.getString("No"));
System.out.println(object.getString("Name"));
}
}
} 출력
1 Adithya 2 Jai 3 Raja
예시 2
import org.json.*;
public class StringToJsonObjectTest {
public static void main(String[] args) {
String str = "{\"name\": \"Raja\", \"technology\": \"Java\"}";
JSONObject json = new JSONObject(str);
System.out.println(json.toString());
String tech = json.getString("technology");
System.out.println(tech);
}
} 출력
{"name":"Raja","technology":"Java"}
Java