基于json-lib.jar包Json实例程序
1.从头或者从零开始,创建一个JSONObject(Creating a JSONObject from scratch)
实例1:
JSONObject jsonObject = new JSONObject();
jsonObject.element("name", "周星星");
jsonObject.element("sex", "male");
jsonObject.element("age", 18);
jsonObject.element("job", "student");
System.out.println(jsonObject.get("name"));// 周星星
System.out.println(jsonObject.get("job"));// student
System.out.println(jsonObject.getString("sex"));// male
System.out.println(jsonObject.getInt("age"));// 18
实例2:
JSONObject jsonObject = new JSONObject().element("string", "JSON").element("integer", "1").element("double", "2.0").element("boolean", "true");
assertEquals("JSON", jsonObject.getString("string"));
assertEquals(1, jsonObject.getInt("integer"));
assertEquals(2.0d, jsonObject.getDouble("double"), 0d);
assertTrue(jsonObject.getBoolean("boolean"));
注:这是对实例1的一个简化版
2.使用一个JSON格式化字符串来创建一个JSONObject(Creating a JSONObject from a JSON formatted string)
实例1:
String json = "{name:\"周星星\",sex:\"male\",age:18,job:\"student\"}";
JSONObject jsonObject = JSONObject.fromObject(json);
//或者使用 JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON(json);
System.out.println(jsonObject.get("name"));// 周星星
System.out.println(jsonObject.get("job"));// student
System.out.println(jsonObject.getString("sex"));// male
System.out.println(jsonObject.getInt("age"));// 18
实例2:
String str = "{'string':'JSON', 'integer': 1, 'double': 2.0, 'boolean': true}";
JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON(str);
assertEquals("JSON", jsonObject.getString("string"));
assertEquals(1, jsonObject.getInt("integer"));
assertEquals(2.0d, jsonObject.getDouble("double"), 0d);
assertTrue(jsonObject.getBoolean("boolean"));
3.使用一个Map来创建一个JSONObject(Creating a JSONObject from a Map)
实例1:
Map map = new HashMap();
map.put( "string", "JSON" );
map.put( "integer", "1" );
map.put( "double", "2.0" );
map.put( "boolean", "true" );
JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON( map );
assertEquals( "JSON", jsonObject.getString("string") );
assertEquals( 1, jsonObject.getInt("integer") );
assertEquals( 2.0d, jsonObject.getDouble("double"), 0d );
assertTrue( jsonObject.getBoolean("boolean") );
4.使用一个JavaBean来创建一个JSONObject(Creating a JSONObject from a JavaBean)
实例1:
public class Mybean {
private String string;
private int integer;
private double dooble;
private boolean bool;
// getters & setters
}
Mybean bean = new Mybean();
bean.setString("JSON");
bean.setInteger(1);
bean.setDooble(2.0d);
bean.setBool(true);
JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON(bean);
assertEquals("JSON", jsonObject.getString("string"));
assertEquals(1, jsonObject.getInt("integer"));
assertEquals(2.0d, jsonObject.getDouble("dooble"), 0d);
assertTrue(jsonObject.getBoolean("bool"));
由此可见,无论要转换的源是哪种类型,都可以使用(JSONObject) JSONSerializer.toJSON()或JSONObject.fromObject()来转换;
分享到:
相关推荐
NULL 博文链接:https://huibin.iteye.com/blog/680822
在web项目中经常会用到json数据(如:struts2处理请求返回json数据给jsp解析),因此,JSONObject对象是必备的,这时就需要引入相关的jar包了。 json所需要的jar包如下: json-lib-2.4-jdk15.jar json所依赖的jar...
org.json.jsonobject等6个jar压缩包集合。包含JSONObject所必需的6个jar包: commons-beanutils-1.7.0.jar commons-collections-3.1.jar commons-lang-2.5.jar commons-logging.jar ezmorph-1.0.3.jar(这里是ezmorph...
解决JsonObject.fromObject方法,和转换List,String>> 型数据的JsonArray.fromObject()方法
java开发中需要jsonObject,jsonArray需要用到6个jar包,net.sf.json.JSONObject commons-beanutils-1.7.0.jar commons-collections-3.1.jar commons-lang-2.5.jar commons-logging.jar ezmorph-1.0.3.jar json-lib-...
org.json.JSONObject的6个jar包 commons-beanutils;commons-collections;commons-lang;commons-logging;ezmorph;json-lib
net.sf.json.JSONObject所需要的jar包 commons-beanutils-1.9.3.jar commons-collections-3.2.2 commons-lang-2.6 commons-logging-1.2 ezmorph-1.0.6 json-lib-2.4-jdk15
JSONObject所需要的jar(json-lib.jar及相关jar)
commons-beanutils-1.9.3.jar、commons-collections-3.2.2.jar、commons-lang-2.6.jar、commons-logging-1.2.jar、ezmorph-1.0.6.jar、json-lib-2.4-jdk15.jar
JSONObject所必须的6个jar包: commons-beanutils-1.7.0.jar commons-collections-3.1.jar commons-lang-2.5.jar commons-logging.jar ezmorph-1.0.3.jar json-lib-2.1-jdk15.jar
最新的json-lib,方便地处理jsonobject 资源清单 json-lib-2.3-jdk15.jar 文档: json-lib-2.3-jdk15-javadoc.jar 依赖: ezmorph-1.0.6.jar commons-beanutils-1.8.3.jar commons-lang-2.5.jar morph-1.1.1.jar ...
jar包如下:commons-beanutils-1.7,commons-collections,commons-lang,commons-logging-1.1.1,ezmorph, gson-2.2.1,gson-2.2.1-sources,json-lib-2.2.2-jdk15
基于json-lib.jar包 创建JSONObject的四个常用方法 基于json-lib.jar包Json实例程序
打包了最新版本的json-lib.jar及其依赖包。里面包括:json-lib-2.4-jdk15.jar、commons-beanutils-1.9.jar、commons-collections-3.2.jar、commons-lang-2.6.jar、commons-logging-1.2.jar、ezmorph-1.0.6.jar、...
有关json-lib-2.2.3-jdk15.jar使用需要的一些jar包
commons-beanutils-1.8.0.jar、commons-collections-3.2.1.jar、commons-lang-2.5.jar、commons-logging-1.1.1.jar、ezmorph-1.0.6.jar、json-lib-2.4-jdk15.jar
在java中使用json需要引入以下六个jar包,分别为: commons-lang-2.4.jar commons-collections-3.2.1.jar commons-beanutils-1.8.3.jar json-lib-2.4-jdk15.jar ezmorph-1.0.6.jar commons-logging-1.1.3.jar ...
json-lib-2.4-jdk15的jar包和源码,便于大家使用net.sf.json.JSONObject及阅读源码。 资源的官方网站http://json-lib.sourceforge.net/
json-lib完整包, commons-beanutils.jar, commons-collections.jar, commons-lang-2.4.jar, commons-logging.jar, ezmorph-1.0.4.jar, json-lib-2.3-jdk15.jar, 以及一个小的测试类。