package temp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONObject;
/**
* @author weiyaqiang
*
*/
public class TestJson {
public static void main(String[] args) {
/**
* map和json对象之间的转换
*/
Map<String, Object> map=new HashMap<String, Object>();
map.put("name", "zhangsan");
map.put("age", 25);
JSONObject jsonObj=new JSONObject(map);
Object age = jsonObj.get("name");
Map<String, Object> map2 = jsonObj.toMap();
System.out.println("map2="+map2.toString());
/**
* List和json数组之间的转换
*/
List list=new ArrayList();
list.add(1);
list.add(2);
JSONArray array=new JSONArray(list);
System.out.println("jsonArray="+list.get(0));
List<Object> list2 = array.toList();
System.out.println("lsit2=="+list2.toString());
/**
* 字符串和json对象之间的转换
*/
String string= "{ \"name\":\"zhangsan\"; \"age\":12}";
JSONObject jsonObject=new JSONObject(string);
String string2 = jsonObject.toString();
System.out.println("string2="+string2);
/**
* 对象转成json对象
*/
Student stu=new Student("lisi",25);
JSONObject jsonStu=new JSONObject(stu);
}
}
JSON转换(Map,List,String,Object)
最新推荐文章于 2023-09-14 09:14:55 发布