JSON-4/6(JSON的生成与管理 )

一、生成JSON

test01:

	//生成JSONObject
	public static void test01()
	{
		JSONObject j1=new JSONObject();
		j1.put("name", "郭少");
		j1.put("id", 998856);
		j1.put("sex", true);
		j1.put("phone", "15199978652");
		String jsonstr=j1.toString(2);   //缩进2
		System.out.println(jsonstr);
	}

 

 ***********************************************************************************************************************************************

test02:

//生成JSONArray:元素是字符串
	public static void test02()
	{
		JSONArray j=new JSONArray();
		j.put("shao");
		j.put("wang");
		j.put("li");
		j.put("guo");
		String jsonstr=j.toString(2);  //缩进2
		System.out.println(jsonstr);
	}

 

 ***********************************************************************************************************************************************

test03:

	//POJO->JSONObject
	//如果是POJO对象(已经添加getter),则可直接转成JSONObject
	public static void test03()
	{
		Student stu=new Student("郭少",159637,true,"15198874562");
		
		JSONObject j=new JSONObject(stu);
		String jsonstr=j.toString(2);  //缩进2
		System.out.println(jsonstr);
	}

 ***********************************************************************************************************************************************

test04:

//List->JSONArray
	public static void test04()
	{
		List<Student> sss=new ArrayList<>();
		sss.add(new Student("guo", 1238901, true, "13810012345"));
		sss.add(new Student("wang", 1238902, true, "13456678895"));
		sss.add(new Student("qian", 1238903, false, "1381432435"));
		sss.add(new Student("chen", 1238904, true, "13342353446"));
		
		JSONArray jarray=new JSONArray();
		for(Student s:sss)
		{
			JSONObject j1=new JSONObject(s);
			jarray.put(j1);
		}
		
		String jsonstr=jarray.toString(2);//缩进2
		System.out.println(jsonstr);
	}

 

*********************************************************************************************************************************************** 

test05:

//List->JSONArray
	public static void test05()
	{
		List<Student> sss = new ArrayList();
		sss.add(new Student("shao", 1238901, true, "13810012345"));
		sss.add(new Student("wang", 1238902, true, "13456678895"));
		sss.add(new Student("qian", 1238903, false, "1381432435"));
		sss.add(new Student("chen", 1238904, true, "13342353446"));
		
		// 直接把ArrayList转成JSON,前提是ArrayList里的元素是可以转化的
		JSONArray jarray = new JSONArray(sss);

		String jsonstr = jarray.toString(2); // 缩进2
		System.out.println(jsonstr);
	}
	public static void main(String[] args)
	{
		test04();
	}

 

*********************************************************************************************************************************************** *********************************************************************************************************************************************** ***********************************************************************************************************************************************  

二、解析JSON 

package json04;

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

public class ParseJSON 
{
	
	//如果jsonstr是一个Object
	public static void test1(String jsonstr)throws  Exception
	{
		JSONObject j=new JSONObject(jsonstr);//解析jsonstr文本,转成JSONObject
		String name=j.getString("name");
		int id=j.getInt("id");
		boolean sex=j.getBoolean("sex");
		String phone=j.getString("phone");
		//还可以复杂性的字段,逐层提取即可
		//JSONObject subobj=j.getJSONObject("xxx");
		//JSONArray subarray=j.getJSONArray("yyy");
	}
	
	//如果jsonstr是一个Array
	public static void test2(String jsonstr)throws  Exception
	{
		JSONArray j=new JSONArray(jsonstr);
		for(int i=0;i<jsonstr.length();i++)
		{
			JSONObject j1=j.getJSONObject(i);
			String name=j1.getString("name");
			int id=j1.getInt("id");
			boolean sex=j1.getBoolean("sex");
			String phone=j1.getString("phone");
		}
	}
	public static void main(String[] args)
	{
		
	}
}

师傅:邵发老师

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值