前端之家收集整理的这篇文章主要介绍了
JSONArray JSONObjec,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
public class JsonTest {
private static JSONObject createJSONObject(){
JSONObject jsonObject = new JSONObject();
jsonObject.put("username","liming");
jsonObject.put("sex","男");
jsonObject.put("username","liming");
jsonObject.put("qq","123456");
jsonObject.put("scores","100");
return jsonObject;
}
public static void main(String args[]){
JSONObject jsonObject = JsonTest.createJSONObject();
Boolean isArray = jsonObject.isArray();
Boolean isEmpty = jsonObject.isEmpty();
Boolean isNull = jsonObject.isNullObject();
System.out.println("isArray--"+isArray+" isEmpty--"+isEmpty+" isNull--"+isNull);
System.out.println("jsonObject=="+jsonObject);
jsonObject.element("addr","河北保定");
jsonObject.put("addr2","hebeibaoding");
System.out.println("new jsonObject=="+jsonObject);
JSONArray jsonArray = new JSONArray();
jsonArray.add(0,"the frist jsonarray");
jsonArray.add(1,"the second jsonarray");
jsonObject.put("jsonArray",jsonArray);
System.out.println(" new jsonObject+jsonarray==="+jsonObject);
// 根据key返回一个字符串
String username = (String) jsonObject.getString("username");
System.out.println("username========"+username);
// 把字符转换为 JSONObject
String temp = jsonObject.toString();
System.out.println();
System.out.println("temp=="+temp);
JSONObject object = JSONObject.fromObject(temp);
System.out.println();
System.out.println();
System.out.println("object=="+object.get("username"));
String demo = "{'username':'liming22','sex':'男22'}";
JSONObject jsonObject2 = JSONObject.fromObject(demo);
System.out.println("jsonObject2=="+jsonObject2);
System.out.println(jsonObject2.get("username"));
}
}
原文链接:https://www.f2er.com/json/289224.html