返回json

	@Override
	public String getChannelInfosResultFromJson(List<ChannelInfo> channelInfos)
			throws IOException {
		JSONArray js = new JSONArray();
		js.addAll(channelInfos);

		return js.toJSONString();
	}

public String taskJsonData(String taskId) {
				JSONArray list = JSONArray.fromObject(resultUI.getResultList());
		JSONObject json = new JSONObject();
		json.put("list", list);
		return json.toString();
	}

  

导的包

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

 maven包

<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.5.0</version>
</dependency>

 2:返回嵌套的json

Map map = new HashMap();
		Map mapa = new HashMap();
		Map mapb = new HashMap();
		map.put("data", mapa);
		map.put("indata", mapb);
		mapa.put("a", 1);
		mapa.put("b", 2);
		mapb.put("c", 3);
		mapb.put("d", 4);
		String jo = JSONObject.valueToString(map);
		System.out.println(jo);

 结果:(不保证与map中的顺序一致)

{"data":{"b":2,"a":1},"indata":{"d":4,"c":3}}

 maven:

写道
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20131018</version>
</dependency>

 

你可能感兴趣的:(返回JSON)