日期:2014-05-16  浏览次数:20423 次

json学习之二:JSONObject对象使用
为了使用JSONObject对象,我们要引入"net.sf.json"包。
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class JsonTest {

	public static void main(String[] args) throws Exception {
		//创建JSONObject对象 通过put(Object key,Object value)方法添加元素
		JSONObject json = new JSONObject();
		json.put("version", "1.1.0");
		json.put("host", "maps.google.com");
		json.put("home_mobile_country_code", 460);// 国家代码
		json.put("home_mobile_network_code", 0);// 移动运营商代码
		json.put("radio_type", "gsm");
		json.put("request_address", true);
		json.put("address_language", "zh_CN");
		
		JSONArray jsoncells = new JSONArray();
		
		JSONObject jsoncell = new JSONObject();
		jsoncell.put("mobile_country_code", 460);// 国家代码,mcc
		jsoncell.put("mobile_network_code", 0);// 移动运营商代码,mnc
		jsoncell.put("location_area_code", 42246);// 位置区域代码,lac LAC 42246
		jsoncell.put("cell_id", 21379917);// 移动基站id CID 21379917
		jsoncell.put("timing_advance",5555);
		//将JSONObject对象添加到JSONArray中
		jsoncells.add(jsoncell);
		//将JSONArray对象添加到JSONObject中
		json.put("cell_towers", jsoncells);

		System.out.println(json.toString());	    

	}
}

API参考:
http://json-lib.sourceforge.net/apidocs/jdk15/index.html
1 楼 zljerityzljerity 2012-08-01  
JSONObject.fromObject(line)这段代码的意思是什么,它得到的是一个什么东东?