package test;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import net.sf.json.JSONArray;
public class TestJson {
String a;
String b;
public String getA() {
return a;
}
public void setA(String a) {
this.a = a;
}
public String getB() {
return b;
}
public void setB(String b) {
this.b = b;
}
@Override
public String toString() {
return "TestJson [a=" + a + ", b=" + b + "]";
}
public TestJson(){
}
public TestJson(String a,String b){
this.a=a;
this.b=b;
}
public static void main(String[] args) {
List<TestJson> ls=new ArrayList<TestJson>();
ls.add(new TestJson("1","2"));
ls.add(new TestJson("3","4"));
byte[] te=getBuffers(ls);
String str=new String(te);
System.out.println(str);
}
private static byte[] getBuffers(List<TestJson> ls) {
ByteBuffer buffer = null ;
String json = JSONArray.fromObject(ls).toString();
System.out.println(json);
try {
buffer = ByteBuffer.allocate(json.getBytes("UTF-8").length);
buffer.put(json.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
if(buffer!=null){
return buffer.array();
}else{
return new byte[0];
}
}
}