json数据的封装

package com.ai.kfk.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

public class Test {
	public static void main(String[] args) {
	    try {
			JSONObject  json1 = new JSONObject();
			//--------------------------------------
			JSONObject  json2 = new JSONObject();
			json2.put("json2Key1", "json2Value1");
			json2.put("json2Key2", 15);
			json2.put("json2Key3", true);
			//--------------------------------------
			JSONObject  json3 = new JSONObject();			
			List<JSONObject> list = new ArrayList<JSONObject>();
			JSONObject  json4 = new JSONObject();
			json4.put("json4Key1", "json4Value1");
			json4.put("json4Key2", 15);
			json4.put("json4Key3", true);
			JSONObject  json5 = new JSONObject();
			json5.put("json5Key1", "json5Value1");
			json5.put("json5Key2", 15);
			json5.put("json5Key3", true);
			JSONObject  json6 = new JSONObject();
			json6.put("json6Key1", "json6Value1");
			json6.put("json6Key2", 15);
			json6.put("json6Key3", true);
			list.add(json4);
			list.add(json5);
			list.add(json6);
			json3.put("json3Key1", list);
			//--------------------------------------
			json1.put("json1Key1", "json1Value1");
			json1.put("json1Key2", 15);
			json1.put("json1Key3", true);
			json1.put("json1Key4", json2);
			json1.put("json1Key5", json3);
			
			System.out.println(json1);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    
	}
}

控制台打印:

{"json1Key1":"json1Value1","json1Key5":{"json3Key1":[{"json4Key3":true,"json4Key2":15,"json4Key1":"json4Value1"},{"json5Key2":15,"json5Key3":true,"json5Key1":"json5Value1"},{"json6Key2":15,"json6Key1":"json6Value1","json6Key3":true}]},"json1Key4":{"json2Key1":"json2Value1","json2Key2":15,"json2Key3":true},"json1Key3":true,"json1Key2":15}


显示json数据:


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值