下载依赖 jar 包   

json.jar 


package com.json1;

import org.json.JSONException;
import org.json.JSONObject;

public class Test
{
	public static void main(String[] args)
	{
		/*
		 题目: 将下面的JSON字符串 解析并打印出来 key=value
			{name:'李俊',age:25,marriage:true,money:1000.5}
		 */
		
		try
		{
			String str="{name:'李俊',age:25,marriage:true,money:1000.5}";
			//放到 JSONObject 里面解析
			JSONObject jsonObj=new JSONObject(str);
			//通过 key 获得 对应的 value 值
			String name=jsonObj.getString("name");
			int age=jsonObj.getInt("age");
			boolean marriage=jsonObj.getBoolean("marriage");
			double money=jsonObj.getDouble("money");
			
			System.out.println(name+","+age+","+marriage+","+money);
		} catch (JSONException e)
		{
			e.printStackTrace();
		}
	}
}





package com.json2;

import org.json.JSONException;
import org.json.JSONObject;

public class Test
{
	public static void main(String[] args)
	{
		/*
		 题目: 将下面的JSON字符串 解析并打印出来
			{name:'李俊',age:25,address:{city:'北京',street:'回龙观',community:'新龙城',floor:10}}
		 */
		
		
		try
		{
			String str="{name:'李俊',age:25,address:{city:'北京',street:'回龙观',community:'新龙城',floor:10}}";
			//最外面一层 是一个 JSONObject
			JSONObject jsonObj=new JSONObject(str);
			String name=jsonObj.getString("name");
			int age=jsonObj.getInt("age");
			
			//获得嵌套的 JSONObject
			JSONObject addressObj=jsonObj.getJSONObject("address");
			String city=addressObj.getString("city");
			String street=addressObj.getString("street");
			String community=addressObj.getString("community");
			int floor=addressObj.getInt("floor");
			
			System.out.println(name);
			System.out.println(age);
			System.out.println("地址:"+city+" "+street+" "+community+" "+floor);
			
		} catch (JSONException e)
		{
			e.printStackTrace();
		}
	}
}






package com.json3;

import org.json.JSONArray;
import org.json.JSONException;

public class Test
{
	public static void main(String[] args)
	{
		/*
		 题目: 将下面的JSON字符串 解析并打印出来
			['篮球','乒乓球','足球','橄榄球','棒球','台球','高尔夫球']
		 */
		
		try
		{
			String str="['篮球','乒乓球','足球','橄榄球','棒球','台球','高尔夫球']";
			//用 JSONArray 数组解析
			JSONArray jsonArray=new JSONArray(str);
			
			for(int i=0;i<jsonArray.length();i++)
			{
				String value=jsonArray.getString(i);
				System.out.println(value);
			}
			
		} catch (JSONException e)
		{
			e.printStackTrace();
		}
	}
}





package com.json4;

import org.json.JSONArray;
import org.json.JSONException;

public class Test
{
	public static void main(String[] args)
	{
		/*
		 题目: 将下面的JSON字符串 解析并打印出来
			[['乒乓球','台球','高尔夫球'],['足球','橄榄球','棒球']]
		 */
		String str="[['乒乓球','台球','高尔夫球'],['足球','橄榄球','棒球']]";
		//解析 第一层
		try
		{
			JSONArray jsonArray=new JSONArray(str);
			for(int i=0;i<jsonArray.length();i++)
			{
				//['乒乓球','台球','高尔夫球']
				JSONArray childArray=jsonArray.getJSONArray(i);
				for(int j=0;j<childArray.length();j++)
				{
					String value=childArray.getString(j);
					System.out.print(value+" ");
				}
				//换一行
				System.out.println("");
			}
			
		} catch (JSONException e)
		{
			e.printStackTrace();
		}
	}
}






package com.json5;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Test
{
	public static void main(String[] args)
	{
		/*
		 1.将下面的JSON字符串 解析并打印出来
				{name:'李俊',age:25,address:{description:'北京 回龙观 新龙城',floor:10},like:['唱歌','画画','旅游']}
		 */
		String str="{name:'李俊',age:25,address:{description:'北京 回龙观 新龙城',floor:10},like:['唱歌','画画','旅游']}";
		//JSONObject 解析
		try
		{
			JSONObject jsonObj=new JSONObject(str);
			String name=jsonObj.getString("name");
			int age=jsonObj.getInt("age");
			System.out.println(name+","+age);
			
			//地址是  JSONObject
			JSONObject addressObj=jsonObj.getJSONObject("address");
			String description=addressObj.getString("description");
			int floor=addressObj.getInt("floor");
			System.out.println(description+","+floor);
			
			//爱好是 JSONArray
			JSONArray likeArray=jsonObj.getJSONArray("like");
			for(int i=0;i<likeArray.length();i++)
			{
				String value=likeArray.getString(i);
				System.out.println(value);
			}
			
		} catch (JSONException e)
		{
			e.printStackTrace();
		}
	}
}






package com.json6;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Test
{
	public static void main(String[] args)
	{
		/*
		 2. 将下面的JSON字符串 解析并打印出来
				[{start:'北京',end:'上海',price:105},{start:'北京',end:'岳阳',price:175},{start:'北京',end:'广州',price:251}]
		 */
		String str="[{start:'北京',end:'上海',price:105},{start:'北京',end:'岳阳',price:175},{start:'北京',end:'广州',price:251}]";
		//最外层 JSONArray
		try
		{
			JSONArray jsonArray=new JSONArray(str);
			for(int i=0;i<jsonArray.length();i++)
			{
				JSONObject jsonObj=jsonArray.getJSONObject(i);
				String start=jsonObj.getString("start");
				String end=jsonObj.getString("end");
				int price=jsonObj.getInt("price");
				
				System.out.println(start+","+end+","+price);
			}
			
		} catch (JSONException e)
		{
			e.printStackTrace();
		}
	}
}