package javaTest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/**
* @author xuhang
*
*/
public class XuTest1 {
public static void main(String[] args) {
// 对象转json字符串
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map1 = new HashMap<String, Object>();// 对象1
Map<String, Object> map2 = new HashMap<String, Object>();// 对象2
map1.put("aaa", "liaaa");
map1.put("bbb", "libbb");
map2.put("ccc", "liccc");
map2.put("ddd", "liddd");
list.add(map1);
list.add(map2);
JSONArray jsonArrayrr = JSONArray.fromObject(list);
System.out.println(jsonArrayrr.toString() + "=======================;;;;========");
// json字符串转复杂对象=====begin
// JSONArray
String jsonArrayData = "[{\"a1\":\"12\",\"b1\":\"112\",\"c1\":\"132\",\"d1\":\"134\"},{\"a2\":\"12\",\"b2\":\"112\",\"c2\":\"132\",\"d2\":\"134\"},{\"a3\":\"12\",\"b3\":\"112\",\"c3\":\"132\",\"d3\":\"134\"}]";
JSONArray jsonArray = JSONArray.fromObject(jsonArrayData);
List<Map<String, Object>> mapListJson = (List) jsonArray;
for (int i = 0; i < mapListJson.size(); i++) {
Map<String, Object> obj = mapListJson.get(i);
for (Entry<String, Object> entry : obj.entrySet()) {
String strkey1 = entry.getKey();
Object strval1 = entry.getValue();
System.out.println("KEY:" + strkey1 + " --> Value:" + strval1 + "\n");
}
}
System.out.println("JSONArray=============================================================");
// JSONObject
String jsonObjectData = "{\"data1\":{\"a1\":\"12\",\"b1\":\"112\",\"c1\":\"132\",\"d1\":\"134\"},\"data2\":{\"a2\":\"12\",\"b2\":\"112\",\"c2\":\"132\",\"d2\":\"134\"},\"data3\":{\"a3\":\"12\",\"b3\":\"112\",\"c3\":\"132\",\"d3\":\"134\"}}";
JSONObject jsonObject = JSONObject.fromObject(jsonObjectData);
Map<String, Object> mapJson = JSONObject.fromObject(jsonObject);
for (Entry<String, Object> entry : mapJson.entrySet()) {
Object strval1 = entry.getValue();
JSONObject jsonObjectStrval1 = JSONObject.fromObject(strval1);
Map<String, Object> mapJsonObjectStrval1 = JSONObject.fromObject(jsonObjectStrval1);
System.out.println("KEYw:" + entry.getKey() + " --> Valuew:" + entry.getValue() + "\n");
for (Entry<String, Object> entry1 : mapJsonObjectStrval1.entrySet()) {
System.out.println("KEY:" + entry1.getKey() + " --> Value:" + entry1.getValue() + "\n");
}
}
System.err.println("JSONObject****************************************************************");
// ==============end
// 判断是否是json格式字符串======begin
String iioo = "{\"lng\":125.338965,\"lat\":43.814968,\"userid\":362,\"shopid\":27}";
JsonToMap jsonToMap = new JsonToMap();
boolean ttt = jsonToMap.isJson(iioo);
System.err.println("ttt:" + ttt);
if (ttt == false) {
return;
}
JSONObject jsonObject11 = JSONObject.fromObject(iioo);
Map<String, Object> mapJson11 = JSONObject.fromObject(jsonObject11);
for (Map.Entry<String, Object> entry : mapJson11.entrySet()) {
System.err.println("key:" + entry.getKey());
System.err.println("val:" + entry.getValue());
}
}
public boolean isJson(String content) {
try {
JSONObject jsonObject11 = JSONObject.fromObject(content);
return true;
} catch (Exception e) {
return false;
}
}
}