JSONObject
类:产生一个JSON
实例对象JSONArray
类:产生一个JSON
实例数组public Object put(Object key, Object value)
:在一个JSONObject
实例对象中添加一个键值对public boolean add(Object value)
:在JSONArray
数组中添加一个对象public static JSONArray fromObject(Object object)
:将一个字符串或JSONObject
对象转换成JSON
数据类型的数组
public class Main {
public static void main(String[] args) {
String json = "[{'day1':'work','day2':26},{'day1':123,'day2':26}]";
JSONArray jsonArray = JSONArray.fromObject(json);
System.out.println(jsonArray);
}
}
public JSONObject getJSONObject(int index)
:获取JSONArray
数组中的指定下标的对象
package com.company;
import net.sf.json.JSONArray;
public class Main {
public static void main(String[] args) {
String json = "[{'day1':'work','day2':26},{'day1':123,'day2':26}]";
JSONArray jsonArray = JSONArray.fromObject(json);
System.out.println(jsonArray.getJSONObject(0));
}
}