JSONObject obj =newJSONObject(); obj.put("sex", "male"); obj.put("age", 22); obj.put("is_student",true); obj.put("hobbies",newString[] {"hiking", "swimming"});//调用toString()方法可直接将其内容打印出来System.out.println(obj.toString()); 结果: {"hobbies":["hiking","swimming...
Admin admin=newAdmin ();// Admin 实体类 String s1 = JSONObject.toJSONString(admin); JSONObject params = JSONObject.parseObject(s1); JSONObject对象转实体类: 1 2 JSONObject obj =newJSONObject(); Admin admin = obj.toJavaObject(Admin.class);// 对应实体类 String转JSONObject对象: 1 JSONObje...
"male");data.put("age",22);data.put("is_student",true);data.put("hobbies",newString[]{"hiking","swimming"});JSONObject obj=newJSONObject(data);// 或是下面这种写法,将 java 对象转换为 json 对象JSONObject obj=JSONObject.fromObject(data);...
importorg.json.JSONObject;publicclassMain{publicstaticvoidmain(String[]args){// 方法一:使用无参构造函数初始化JSONObjectobj1=newJSONObject();// 方法二:使用字符串初始化StringjsonString="{\"key1\": \"value1\", \"key2\": \"value2\"}";JSONObjectobj2=newJSONObject(jsonString);// 方法三...
JSONObject obj2=new JSONObject(); obj2.put("phone","123456"); obj2.put("zip","7890"); obj2.put("contact",obj2); System.out.print(obj2); } //复合类型bean转成成json @Test public void testBeadToJSON(){ MyBean bean = new MyBean(); ...
json.JSONObject; public class JSONObjectSample { public static void main(String[] args) { createJson(); } private static void createJson() { JSONObject obj = new JSONObject(); obj.put("name", "John"); obj.put("sex", "male"); obj.put("age", 22); obj.put("is_student", true...
import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class Json { public static void main(String[] args) { //创建JSONObject JSONObject json = new JSONObject(); //添加属性 json.put("username", "张三"); json.put("password", "123"); //打印 System.out.println(json);...
jsonArray.put("value");从JSONArray中获取元素:String value = jsonArray.getString(0);遍历JSONArray中的元素:for (int i = 0; i < jsonArray.length(); i++){ String value = jsonArray.getString(i);}二、JSONObject 创建JSONObject对象:JSONObject jsonObject = new JSONObject()...
JsonObjectcan be written to JSON as follows: JsonWriter writer = ... JsonObject obj = ...; writer.writeObject(obj); JsonObjectvalues can beJsonObject,JsonArray,JsonString,JsonNumber,JsonValue.TRUE,JsonValue.FALSE,JsonValue.NULL. These values can be accessed using various accessor methods. ...
从标准JSON字符串中提取指定字符串。本文介绍GET_JSON_OBJECT函数在JSON和STRING类型入参下的使用方法和注意事项。