java中JSON将数组对象转换成JSON字符串输出实例

先上代码和结果

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

import net.sf.json.JSONArray;

public class TestJsonObject {

	public static void main(String args[]){
		ArrayList 	 users=new  ArrayList();
		users.add(new User("zhangsan",21));
		users.add(new User("lisi",18));
		users.add(new User("wangwu",32));
		
		JSONArray result = JSONArray.fromObject(users);
		System.out.println(result);
		
	}
	
}

public class User {

	private String name;
	private int age;

	public User(String name, int age) {
		this.name = name;
		this.age = age;
	}
}
输出结果

[{"name":"zhangsan","value":21},{"name":"lisi","value":18},{"name":"wangwu","value":32}]

在这个例子中,json将users数组对象转化为json字符串输出了

你可能感兴趣的:(java)