java json 转换
Package net.sf.json:
http://json-lib.sourceforge.net/apidocs/jdk15/net/sf/json/package-summary.html
JAVA中如何将一个json形式的字符串转为json对象?
import net.sf.json.JSONObject;
public class test {
public static void main(String[] args) {
String str = "{\"a1\":\"b\", \"c\":\"d\"}";
JSONObject a =JSONObject.fromObject(str);
System.out.println(a); // {"c":"d","a":"b"}
System.out.println(a.get("a1")); // d
}
}
直接把字符串作为参数交给JSONObject就行了……
List<SqlTableBean> list = new ArrayList<SqlTableBean>();
list = connectionDB.getListFromDB();
JSONArray a =JSONArray.fromObject(list);
System.out.println(a.toString());
//print: [{"num1":"568","num2":"568","product_ctg":"纸尿裤"},{"num1":"367","num2":"367","product_ctg":"营养米粉"},{"num1":"274","num2":"274","product_ctg":"
营养面条"},{"num1":"186","num2":"186","product_ctg":"营养泥"},{"num1":"156","num2":"156","product_ctg":"奶瓶"},{"num1":"137","num2":"137","product_ct
g":"摇铃/床铃"},{"num1":"137","num2":"137","product_ctg":"奶嘴"},{"num1":"131","num2":"131","product_ctg":"油类"},{"num1":"112","num2":"112","produc
t_ctg":"长牙期食品"},{"num1":"110","num2":"110","product_ctg":"毛绒玩具"},{"num1":"95","num2":"95","product_ctg":"肉松/肉酥"}]
=======================================================
String[] a={"看见快乐","def","g345"};
JSONArray json=new JSONArray().fromObject(a);
System.out.println(json.toString());
Console:
["看见快乐","def","g345"]