一、生成JSON
test01:
//生成JSONObject
public static void test01()
{
JSONObject j1=new JSONObject();
j1.put("name", "郭少");
j1.put("id", 998856);
j1.put("sex", true);
j1.put("phone", "15199978652");
String jsonstr=j1.toString(2); //缩进2
System.out.println(jsonstr);
}
***********************************************************************************************************************************************
test02:
//生成JSONArray:元素是字符串
public static void test02()
{
JSONArray j=new JSONArray();
j.put("shao");
j.put("wang");
j.put("li");
j.put("guo");
String jsonstr=j.toString(2); //缩进2
System.out.println(jsonstr);
}
***********************************************************************************************************************************************
test03:
//POJO->JSONObject
//如果是POJO对象(已经添加getter),则可直接转成JSONObject
public static void test03()
{
Student stu=new Student("郭少",159637,true,"15198874562");
JSONObject j=new JSONObject(stu);
String jsonstr=j.toString(2); //缩进2
System.out.println(jsonstr);
}
***********************************************************************************************************************************************
test04:
//List->JSONArray
public static void test04()
{
List<Student> sss=new ArrayList<>();
sss.add(new Student("guo", 1238901, true, "13810012345"));
sss.add(new Student("wang", 1238902, true, "13456678895"));
sss.add(new Student("qian", 1238903, false, "1381432435"));
sss.add(new Student("chen", 1238904, true, "13342353446"));
JSONArray jarray=new JSONArray();
for(Student s:sss)
{
JSONObject j1=new JSONObject(s);
jarray.put(j1);
}
String jsonstr=jarray.toString(2);//缩进2
System.out.println(jsonstr);
}
***********************************************************************************************************************************************
test05:
//List->JSONArray
public static void test05()
{
List<Student> sss = new ArrayList();
sss.add(new Student("shao", 1238901, true, "13810012345"));
sss.add(new Student("wang", 1238902, true, "13456678895"));
sss.add(new Student("qian", 1238903, false, "1381432435"));
sss.add(new Student("chen", 1238904, true, "13342353446"));
// 直接把ArrayList转成JSON,前提是ArrayList里的元素是可以转化的
JSONArray jarray = new JSONArray(sss);
String jsonstr = jarray.toString(2); // 缩进2
System.out.println(jsonstr);
}
public static void main(String[] args)
{
test04();
}
*********************************************************************************************************************************************** *********************************************************************************************************************************************** ***********************************************************************************************************************************************
二、解析JSON
package json04;
import org.json.JSONArray;
import org.json.JSONObject;
public class ParseJSON
{
//如果jsonstr是一个Object
public static void test1(String jsonstr)throws Exception
{
JSONObject j=new JSONObject(jsonstr);//解析jsonstr文本,转成JSONObject
String name=j.getString("name");
int id=j.getInt("id");
boolean sex=j.getBoolean("sex");
String phone=j.getString("phone");
//还可以复杂性的字段,逐层提取即可
//JSONObject subobj=j.getJSONObject("xxx");
//JSONArray subarray=j.getJSONArray("yyy");
}
//如果jsonstr是一个Array
public static void test2(String jsonstr)throws Exception
{
JSONArray j=new JSONArray(jsonstr);
for(int i=0;i<jsonstr.length();i++)
{
JSONObject j1=j.getJSONObject(i);
String name=j1.getString("name");
int id=j1.getInt("id");
boolean sex=j1.getBoolean("sex");
String phone=j1.getString("phone");
}
}
public static void main(String[] args)
{
}
}
师傅:邵发老师