JSONObject Parse Text จาก String ผลลัพธ์เป็น Map-like Object JSONArray Parse Text จาก String ผลลัพธ์เป็น Vector-like Object สามารถเพิ่ม JSONArray เข้า JSONObject ได้ โดยสร้าง JSONArray ก่อน แล้วเพิ่มผ่านเมธอด put() ของ JSONObject
Syntax
public JSONObject put(String key, Collection<?> value) throws JSONException
ตัวอย่าง: เพิ่ม JSONArray ใน JSONObject
import org.json.*;
public class AddJSONArrayTest {
public static void main(String[] args) throws JSONException {
JSONArray array = new JSONArray();
array.put("INDIA");
array.put("AUSTRALIA");
array.put("ENGLAND");
JSONObject obj = new JSONObject();
obj.put("COUNTRIES", array);
System.out.println(obj);
}
}
ผลลัพธ์
{"COUNTRIES":["INDIA","AUSTRALIA","ENGLAND"]}
เมธอด put() Overloads:
put(String key, Object value)- ค่าเดี่ยวput(String key, Collection> value)- Collection/Array (Auto-convert เป็น JSONArray)put(String key, Map,?> value)- Map (Auto-convert เป็น JSONObject)putOnce(String key, Object value)- โยน Exception หาก Key ซ้ำ
Dependency: org.json:json