json数据

List<String>转json数据格式

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;

public class JsonListData {

	public static void main(String[] args) {
		List<String> list = new ArrayList<String>();
		list.add("你好");
		list.add("Java");
		list.add("大世界");
		JSONArray jsonArray = JSONArray.fromObject(list);
		System.out.println(jsonArray);//["你好","Java","大世界"]
	}
}

map转json数据格式:

import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class JsonMapData {
	
	public static void main(String[] args) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("hello", "你好");
		map.put("world", "大世界");
		//转化成对象格式是json数据
		JSONObject json = JSONObject.fromObject(map);
		System.out.println(json);//{"world":"大世界","hello":"你好"}
		//转化成数组格式是json数据
		JSONArray jsonArray = JSONArray.fromObject(map);
		System.out.println(jsonArray);//[{"world":"大世界","hello":"你好"}]
	}
}


list<map>格式的json数据:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

public class AAAA {
	public static void main(String[] args) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", "新疆");
		map.put("value", "0");
		Map<String, Object> map1 = new HashMap<String, Object>();
		map1.put("name", "西藏");
		map1.put("value", "0");
		List<Map<String, Object>> list1 = new ArrayList<Map<String, Object>>();
		list1.add(map);
		list1.add(map1);
		JSONArray ob = JSONArray.fromObject(list1);
		System.out.println(ob);//[{"name":"新疆","value":"0"},{"name":"西藏","value":"0"}]
	}
}

数据的封装都是对前台页面数值的处理。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值