JAVA下JSON的类型输出及使用
JSON类型的输出:
import java.util.ArrayList;
import java.util.HashMap;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.json.*;
import java.io.*;
public class CESHI{
public String CESHI_()
{
ArrayList lst=new ArrayList();
HashMap map=new HashMap();
map.put("苹果","G001");
map.put("香蕉","G002");
map.put("西瓜","G003");
map.put("菠萝","G004");
lst.add(map);
return JSONArray.fromObject(lst).toString();
}
}
JSON类型的对象实例化及使用:
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class starts {
public static void main(String[] args) {
JSONArray jsonArray=JSONArray.fromObject(字符串);
for(int i=0;i<jsonArray.size();i++)
{
JSONObject jsonObj=(JSONObject)jsonArray.get(i);
System.out.println(jsonObj.get(KEYNAME));
}
}
}
浙公网安备 33010602011771号