前端之家收集整理的这篇文章主要介绍了
JSONObject 和JSONArray 方法测试,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package com.lei.test
import java.util.ArrayList
import java.util.HashMap
import java.util.List
import java.util.Map
import net.sf.json.JSONArray
import net.sf.json.JSONObject
public class JSONTest {
public static void main(String argsp[]) {
List<String> list = new ArrayList<String>()
list.add("first")
list.add("second")
JSONArray jsonArray2 = JSONArray.fromObject(list)
Map<String,String> map = new HashMap<String,String>()
map.put("name","sad")
map.put("age","2343")
map.put("list",list.toString())
JSONObject jsonObject = JSONObject.fromObject(map)
System.out.println(jsonObject.toString())
System.out.println(jsonArray2.toString())
String s = new String("{\"name\":\"zxc\",\"age\":21}")
JSONObject jsonObject2 = JSONObject.fromObject(s)
String s2 = new String("[\"nihao\",\"wohenhao\"]")
JSONArray jsonArray1 = JSONArray.fromObject(s2)
System.out.println(jsonArray1.toString())
System.out.println(jsonObject2.toString())
}
}