import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class test1 {
/**
* @param <JSONObject>
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//将json对象转换为字符串
JSONObject obj=new JSONObject();
obj.put("ooo", "***");
obj.put("ppp", "###");
JSONObject object=new JSONObject();
object.put("aaa", "123");
object.put("bbb", "456");
object.put("ccc", obj);
System.out.println(object);
//将json数组对象转换为字符串
JSONArray arry=new JSONArray();
JSONObject object1=new JSONObject();
object1.put("aaa", "123");
object1.put("bbb", "456");
JSONObject object2=new JSONObject();
object2.put("ccc", "123");
object2.put("ddd", "456");
arry.add(object1);
arry.add(object2);
System.out.println(arry.toJSONString());
//将字符串转换为JSON对象
String strJson ="{\"aaa\":\"111\",\"bbb\":\"222\",\"ccc\":\"333\",\"ddd\":\"444\"}";
JSONParser parser=new JSONParser();
try {
JSONObject onject3=(JSONObject) parser.parse(strJson);
System.out.println(onject3.toJSONString());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//将字符串转换为JSON数组对象
String jsonStr1 = "[{\"aaa\":\"111\",\"bbb\":\"222\"},{\"aaa\":\"111\",\"bbb\":\"222\"}]";
try {
JSONArray parseObject = (JSONArray)parser.parse(jsonStr1);
System.out.println( parseObject.toJSONString());
} catch (ParseException e) {
e.printStackTrace();
}
}
}