JAVA中JSONObject
在用json时发现两个JSONObject,org.json.simple.JSONObject和org.json.JSONObject,两者有什么优缺?感谢1楼回答。这...
在用json时发现两个JSONObject,org.json.simple.JSONObject和org.json.JSONObject,两者有什么优缺?
感谢1楼回答。这个测试我见过自己也试过,这个测不出,仔细追下代码会发现,两者的操作不一样的。new JSONObject(results.toString()); 和(JSONObject) JSONValue.parse(results.toString()); 展开
感谢1楼回答。这个测试我见过自己也试过,这个测不出,仔细追下代码会发现,两者的操作不一样的。new JSONObject(results.toString()); 和(JSONObject) JSONValue.parse(results.toString()); 展开
3个回答
展开全部
private static String castToJson(Object obj) {
if (obj == null) {
return "null";
} else if (obj instanceof Boolean) {
return obj.toString();
} else if (obj instanceof Integer || obj instanceof Long
|| obj instanceof Float || obj instanceof Double
|| obj instanceof Short || obj instanceof java.math.BigInteger
|| obj instanceof java.math.BigDecimal) {
return obj.toString();
} else if (obj instanceof String) {
String v = (String) obj;
v = v.replaceAll("\\\\", "\\\\\\\\");
v = v.replaceAll("\n", "\\\\n");
v = v.replaceAll("\r", "\\\\r");
v = v.replaceAll("\"", "\\\\\"");
v = v.replaceAll("'", "\\\\\'");
return "\"" + v + "\"";
} else if (obj instanceof java.sql.Date) {
java.text.SimpleDateFormat df = new java.text.SimpleDateFormat(
"yyyy-MM-dd");
java.sql.Date v = (java.sql.Date) obj;
String s = df.format(new java.util.Date(v.getTime()));
return "\"" + s + "\"";
} else if (obj instanceof java.util.Date) {
java.text.SimpleDateFormat df = new java.text.SimpleDateFormat(
"yyyy-MM-dd");
java.util.Date v = (java.util.Date) obj;
String s = df.format(v);
return "\"" + s + "\"";
} else if (obj instanceof java.sql.Timestamp) {
java.text.SimpleDateFormat df = new java.text.SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
java.sql.Timestamp v = (java.sql.Timestamp) obj;
String s = df.format(new java.util.Date(v.getTime()));
return "\"" + s + "\"";
} else {
return null;
}
}
还是用这个自己手都转看着放心,他们那个用着不舒服
if (obj == null) {
return "null";
} else if (obj instanceof Boolean) {
return obj.toString();
} else if (obj instanceof Integer || obj instanceof Long
|| obj instanceof Float || obj instanceof Double
|| obj instanceof Short || obj instanceof java.math.BigInteger
|| obj instanceof java.math.BigDecimal) {
return obj.toString();
} else if (obj instanceof String) {
String v = (String) obj;
v = v.replaceAll("\\\\", "\\\\\\\\");
v = v.replaceAll("\n", "\\\\n");
v = v.replaceAll("\r", "\\\\r");
v = v.replaceAll("\"", "\\\\\"");
v = v.replaceAll("'", "\\\\\'");
return "\"" + v + "\"";
} else if (obj instanceof java.sql.Date) {
java.text.SimpleDateFormat df = new java.text.SimpleDateFormat(
"yyyy-MM-dd");
java.sql.Date v = (java.sql.Date) obj;
String s = df.format(new java.util.Date(v.getTime()));
return "\"" + s + "\"";
} else if (obj instanceof java.util.Date) {
java.text.SimpleDateFormat df = new java.text.SimpleDateFormat(
"yyyy-MM-dd");
java.util.Date v = (java.util.Date) obj;
String s = df.format(v);
return "\"" + s + "\"";
} else if (obj instanceof java.sql.Timestamp) {
java.text.SimpleDateFormat df = new java.text.SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
java.sql.Timestamp v = (java.sql.Timestamp) obj;
String s = df.format(new java.util.Date(v.getTime()));
return "\"" + s + "\"";
} else {
return null;
}
}
还是用这个自己手都转看着放心,他们那个用着不舒服
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
测试方法:
//org.json
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
long in = Calendar.getInstance().getTimeInMillis();
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String reason = request.getParameter("reason");
try {
for (int i = 0; i < 1000; i++) {
JSONObject results = new JSONObject();
results.put("isSuccess", true);
results.put("reason", reason);
JSONObject r = new JSONObject(results.toString());
out.println(r.toString());
}
out.println(Calendar.getInstance().getTimeInMillis() - in);
out.close();
} catch (JSONException e) {
e.printStackTrace();
}
}
//json_simple-1.1
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
long in = Calendar.getInstance().getTimeInMillis();
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String reason = request.getParameter("reason");
for (int i = 0; i < 1000; i++) {
JSONObject results = new JSONObject();
results.put("isSuccess", true);
results.put("reason", reason);
JSONObject r = (JSONObject) JSONValue.parse(results.toString());
out.println(r.toString());
}
out.println(Calendar.getInstance().getTimeInMillis() - in);
out.close();
}
/*测试结果:org.json输入10次,反应时间【16,31,31,31,31,31,31,32,32,31】
json_simple-1.1输入10次,反应时间【47,62,62,62,78,79,78,62,79,63】
测试结论:每次输入的reason参数都不一样,可以得出org.json性能在某些方面超过 json_simple
*/
//org.json
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
long in = Calendar.getInstance().getTimeInMillis();
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String reason = request.getParameter("reason");
try {
for (int i = 0; i < 1000; i++) {
JSONObject results = new JSONObject();
results.put("isSuccess", true);
results.put("reason", reason);
JSONObject r = new JSONObject(results.toString());
out.println(r.toString());
}
out.println(Calendar.getInstance().getTimeInMillis() - in);
out.close();
} catch (JSONException e) {
e.printStackTrace();
}
}
//json_simple-1.1
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
long in = Calendar.getInstance().getTimeInMillis();
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String reason = request.getParameter("reason");
for (int i = 0; i < 1000; i++) {
JSONObject results = new JSONObject();
results.put("isSuccess", true);
results.put("reason", reason);
JSONObject r = (JSONObject) JSONValue.parse(results.toString());
out.println(r.toString());
}
out.println(Calendar.getInstance().getTimeInMillis() - in);
out.close();
}
/*测试结果:org.json输入10次,反应时间【16,31,31,31,31,31,31,32,32,31】
json_simple-1.1输入10次,反应时间【47,62,62,62,78,79,78,62,79,63】
测试结论:每次输入的reason参数都不一样,可以得出org.json性能在某些方面超过 json_simple
*/
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
将Java 集合转换成 json字符串
复制代码 代码如下:
Person p1=new Person();p1.setName("A1");p1.setAge(26);
Person p2=new Person();p2.setName("A2");p2.setAge(23);
List personList=new ArrayList<Person>();
personList.add(p1);
personList.add(p2);
Map personMap=new HashMap<String, Person>();
personMap.put("p1", p1);
personMap.put("p2", p2);
//[{"age":26,"name":"A1"},{"age":23,"name":"A2"}]
JSONArray.fromObject(personList).toString();
JSONSerializer.toJSON(personList)
//[{"p2":{"name":"A2","age":23},"p1":{"name":"A1","age":26}}]
JSONArray.fromObject(personMap).toString();
JSONSerializer.toJSON(personMap)
复制代码 代码如下:
Person p1=new Person();p1.setName("A1");p1.setAge(26);
Person p2=new Person();p2.setName("A2");p2.setAge(23);
List personList=new ArrayList<Person>();
personList.add(p1);
personList.add(p2);
Map personMap=new HashMap<String, Person>();
personMap.put("p1", p1);
personMap.put("p2", p2);
//[{"age":26,"name":"A1"},{"age":23,"name":"A2"}]
JSONArray.fromObject(personList).toString();
JSONSerializer.toJSON(personList)
//[{"p2":{"name":"A2","age":23},"p1":{"name":"A1","age":26}}]
JSONArray.fromObject(personMap).toString();
JSONSerializer.toJSON(personMap)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询