JSON详细学习之JSONObject in JAVA

学习之JSONObject in JAVA

一、JAR包简介

要使程序可以运行必须引入JSON-lib包,JSON-lib包同时依赖于以下的JAR包:

1.commons-lang.jar

2.commons-beanutils.jar

3.commons-collections.jar

4.commons-logging.jar

5.ezmorph.jar

6.json-lib-2.2.2-jdk15.jar

二、 JSONObject对象使用

JSON- lib包是一个beans,collections,maps,java arrays 和XML和JSON互相转换的包。在本例中,我们将使用JSONObject类创建JSONObject对象,然后我们打印这些对象的值。为了使用 JSONObject对象,我们要引入"net.sf.json"包。为了给对象添加元素,我们要使用put()方法。

Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://www.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" lk_media="yes" lk_mediaid="lk_juiceapp_mediaPopup_1262010943179" flashvars="clipboard=import%20net.sf.json.JSONArray%3B%0Aimport%20net.sf.json.JSONObject%3B%0A%0Apublic%20class%20JSONObjectSample%20%7B%0A%09%0A%09%2F%2F%E5%88%9B%E5%BB%BAJSONObject%E5%AF%B9%E8%B1%A1%0A%09private%20static%20JSONObject%20createJSONObject()%7B%0A%09%09JSONObject%20jsonObject%20%3D%20new%20JSONObject()%3B%0A%09%09jsonObject.put(%22name%22%2C%20%22kevin%22)%3B%0A%09%09jsonObject.put(%22Max.score%22%2C%20new%20Integer(100))%3B%0A%09%09jsonObject.put(%22Min.score%22%2C%20new%20Integer(50))%3B%0A%09%09jsonObject.put(%22nickname%22%2C%20%22picglet%22)%3B%0A%09%09return%20jsonObject%3B%0A%09%7D%0A%09public%20static%20void%20main(String%5B%5D%20args)%20%7B%0A%09%09JSONObject%20jsonObject%20%3D%20JSONObjectSample.createJSONObject()%3B%0A%09%09%2F%2F%E8%BE%93%E5%87%BAjsonobject%E5%AF%B9%E8%B1%A1%0A%09%09System.out.println(%22jsonObject%3D%3D%3E%22%2BjsonObject)%3B%0A%09%09%0A%09%09%2F%2F%E5%88%A4%E8%AF%BB%E8%BE%93%E5%87%BA%E5%AF%B9%E8%B1%A1%E7%9A%84%E7%B1%BB%E5%9E%8B%0A%09%09boolean%20isArray%20%3D%20jsonObject.isArray()%3B%0A%09%09boolean%20isEmpty%20%3D%20jsonObject.isEmpty()%3B%0A%09%09boolean%20isNullObject%20%3D%20jsonObject.isNullObject()%3B%0A%09%09System.out.println(%22isArray%3A%22%2BisArray%2B%22%20isEmpty%3A%22%2BisEmpty%2B%22%20isNullObject%3A%22%2BisNullObject)%3B%0A%09%09%0A%09%09%2F%2F%E6%B7%BB%E5%8A%A0%E5%B1%9E%E6%80%A7%0A%09%09jsonObject.element(%22address%22%2C%20%22swap%20lake%22)%3B%0A%09%09System.out.println(%22%E6%B7%BB%E5%8A%A0%E5%B1%9E%E6%80%A7%E5%90%8E%E7%9A%84%E5%AF%B9%E8%B1%A1%3D%3D%3E%22%2BjsonObject)%3B%0A%09%09%0A%09%09%2F%2F%E8%BF%94%E5%9B%9E%E4%B8%80%E4%B8%AAJSONArray%E5%AF%B9%E8%B1%A1%0A%09%09JSONArray%20jsonArray%20%3D%20new%20JSONArray()%3B%0A%09%09jsonArray.add(0%2C%20%22this%20is%20a%20jsonArray%20value%22)%3B%0A%09%09jsonArray.add(1%2C%22another%20jsonArray%20value%22)%3B%0A%09%09jsonObject.element(%22jsonArray%22%2C%20jsonArray)%3B%0A%09%09JSONArray%20array%20%3D%20jsonObject.getJSONArray(%22jsonArray%22)%3B%0A%09%09System.out.println(%22%E8%BF%94%E5%9B%9E%E4%B8%80%E4%B8%AAJSONArray%E5%AF%B9%E8%B1%A1%EF%BC%9A%22%2Barray)%3B%0A%09%09%2F%2F%E6%B7%BB%E5%8A%A0JSONArray%E5%90%8E%E7%9A%84%E5%80%BC%0A%09%09%2F%2F%7B%22name%22%3A%22kevin%22%2C%22Max.score%22%3A100%2C%22Min.score%22%3A50%2C%22nickname%22%3A%22picglet%22%2C%22address%22%3A%22swap%20lake%22%2C%0A%09%09%2F%2F%22jsonArray%22%3A%5B%22this%20is%20a%20jsonArray%20value%22%2C%22another%20jsonArray%20value%22%5D%7D%0A%09%09System.out.println(jsonObject)%3B%0A%09%09%0A%09%09%2F%2F%E6%A0%B9%E6%8D%AEkey%E8%BF%94%E5%9B%9E%E4%B8%80%E4%B8%AA%E5%AD%97%E7%AC%A6%E4%B8%B2%0A%09%09String%20jsonString%20%3D%20jsonObject.getString(%22name%22)%3B%0A%09%09System.out.println(%22jsonString%3D%3D%3E%22%2BjsonString)%3B%0A%09%7D%0A%7D%0A" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. import net.sf.json.JSONArray;
  2. import net.sf.json.JSONObject;
  3. public class JSONObjectSample{
  4. //创建JSONObject对象
  5. private static JSONObjectcreateJSONObject(){
  6. JSONObjectjsonObject=new JSONObject();
  7. jsonObject.put("name" , "kevin" );
  8. jsonObject.put("Max.score" , new Integer( 100 ));
  9. jsonObject.put("Min.score" , new Integer( 50 ));
  10. jsonObject.put("nickname" , "picglet" );
  11. return jsonObject;
  12. }
  13. public static void main(String[]args){
  14. JSONObjectjsonObject=JSONObjectSample.createJSONObject();
  15. //输出jsonobject对象
  16. System.out.println("jsonObject==>" +jsonObject);
  17. //判读输出对象的类型
  18. boolean isArray=jsonObject.isArray();
  19. boolean isEmpty=jsonObject.isEmpty();
  20. boolean isNullObject=jsonObject.isNullObject();
  21. System.out.println("isArray:" +isArray+ "isEmpty:" +isEmpty+ "isNullObject:" +isNullObject);
  22. //添加属性
  23. jsonObject.element("address" , "swaplake" );
  24. System.out.println("添加属性后的对象==>" +jsonObject);
  25. //返回一个JSONArray对象
  26. JSONArrayjsonArray=new JSONArray();
  27. jsonArray.add(0 , "thisisajsonArrayvalue" );
  28. jsonArray.add(1 , "anotherjsonArrayvalue" );
  29. jsonObject.element("jsonArray" ,jsonArray);
  30. JSONArrayarray=jsonObject.getJSONArray("jsonArray" );
  31. System.out.println("返回一个JSONArray对象:" +array);
  32. //添加JSONArray后的值
  33. //{"name":"kevin","Max.score":100,"Min.score":50,"nickname":"picglet","address":"swaplake",
  34. //"jsonArray":["thisisajsonArrayvalue","anotherjsonArrayvalue"]}
  35. System.out.println(jsonObject);
  36. //根据key返回一个字符串
  37. StringjsonString=jsonObject.getString("name" );
  38. System.out.println("jsonString==>" +jsonString);
  39. }
  40. }
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class JSONObjectSample {
	
	//创建JSONObject对象
	private static JSONObject createJSONObject(){
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("name", "kevin");
		jsonObject.put("Max.score", new Integer(100));
		jsonObject.put("Min.score", new Integer(50));
		jsonObject.put("nickname", "picglet");
		return jsonObject;
	}
	public static void main(String[] args) {
		JSONObject jsonObject = JSONObjectSample.createJSONObject();
		//输出jsonobject对象
		System.out.println("jsonObject==>"+jsonObject);
		
		//判读输出对象的类型
		boolean isArray = jsonObject.isArray();
		boolean isEmpty = jsonObject.isEmpty();
		boolean isNullObject = jsonObject.isNullObject();
		System.out.println("isArray:"+isArray+" isEmpty:"+isEmpty+" isNullObject:"+isNullObject);
		
		//添加属性
		jsonObject.element("address", "swap lake");
		System.out.println("添加属性后的对象==>"+jsonObject);
		
		//返回一个JSONArray对象
		JSONArray jsonArray = new JSONArray();
		jsonArray.add(0, "this is a jsonArray value");
		jsonArray.add(1,"another jsonArray value");
		jsonObject.element("jsonArray", jsonArray);
		JSONArray array = jsonObject.getJSONArray("jsonArray");
		System.out.println("返回一个JSONArray对象:"+array);
		//添加JSONArray后的值
		//{"name":"kevin","Max.score":100,"Min.score":50,"nickname":"picglet","address":"swap lake",
		//"jsonArray":["this is a jsonArray value","another jsonArray value"]}
		System.out.println(jsonObject);
		
		//根据key返回一个字符串
		String jsonString = jsonObject.getString("name");
		System.out.println("jsonString==>"+jsonString);
	}
}

得到JSONObject对象后我们就可以使用它的方法了,可以查看其API,我给出一个在线的API

http://json-lib.sourceforge.net/apidocs/jdk15/index.html

 

阅读更多
个人分类: android
想对作者说点什么? 我来说一句

jsonObject 源码

2011年04月24日 50KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭