基于json-lib.jar包 创建JSONObject的四个常用方法
2015-07-29 16:18
435 查看
(JSONObject) JSONSerializer.toJSON()或JSONObject.fromObject() 差不多是相同的功能
基于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()来转换;
基于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()来转换;
相关文章推荐
- Object-C 字符串
- JSONObject/JSONArray
- JAVA DAO(Data Access Object)的个人总结
- object入门(转)
- [objective-c]使用Lumberjack未定义
- *Delphi容器类之---TList、TStringList、TObjectList,以及一个例程的代码分析
- JSONObject与JSONArray的使用
- JSONObject JSONArray
- iOS开发 Objective-C中@property的属性详解
- 黑马程序员--iOS--Objective-C基础 (一)
- objectARX错误处理函数
- objectARX结果类型码
- Object 为空
- objective-c中的方法重载
- An object provides services(每个对象都提供服务)
- 使用Objective-C的文档生成工具:appledoc
- ObjectiveC开发教程--字符串的连接
- Objective-c里氏对换原则与开闭原则
- Objective-C动态类型(isKindOfClass,isMemberOfClass)
- OC自学的第一天