一、其他对象转为JSONObject
1、String对象转为JSONObject
JSONObject.parseObject(String s)
(s 必须按照JSON格式书写,否则会出现JSON解析错误)
String test = "{\n" +
"\t\"message\": \"success\",\n" +
"\t\"code\": 0,\n" +
"\t\"data\": [\n" +
" {\n" +
" \"test1\": {\n" +
"\t\t\"totalCount\": 11,\n" +
"\t\t\"errorCount\": 1\n" +
"\t},\n" +
" \"test2\": {\n" +
"\t\t\"totalCount\": 10,\n" +
"\t\t\"errorCount\": 1\n" +
"\t}\n" +
"\n" +
"}]\n" +
"}";
JSONObject json =JSONObject.parseObject(test);
json为 :
额外:把String转为JSONArray
JSONObject.parseArray(String s)
额外:把String转为JSONArray
JSONObject.parseArray(String s)
2、简单的实例对象转为JSONObject
public class User {
private String name;
private int age;
public User(String name,int age)
{
this.name=name;
this.age=age;
}
public void SetName(String name)
{
this.name=name;
}
public void SetAge(int age)
{
this.age=age;
}
public String GetName ()
{
return this.name;
}
public int GetAge ()
{
return this.age;
}
public String toString() {
return "{\n" +
"\t\"name\":" +"\""+this.name+"\","+
"\t\"age\" :" +this.age+
"}\n"
;
}
}
这里在实体类中需要有个ToString方法,重写ToString方法把实例对象转为符合JSON格式的字符串,然后利用JSONObject.parseObject(String s)方法
JSONObject json=JSONObject.parseObject(user.ToString())
3、Map对象转为JSONObject
1)、先利用JSONObject.toJSONString(Object o)把o转为String 2)然后利用String转JSONobject的方法转换
HashMap<String,Integer> map=new HashMap<String,Integer>();
map.put("tt",4);
map.put("pp",9);
JSONObject json=JSONObject.parseObject(JSONObject.toJSONString(map));
4、自定义的实例对象转为JSONObject
com.alibaba.fastjson.JSONObject暂时没有对应的方法,需要自己利用反射机制实现
二、JSONObject转为其他对象
对象 user=JSONObject.parseObject(test,对象.class);
1、把JSONObject转为HashMap类型
String test = "{" + "\"name\": \"success\",\n" +
"\"age\": 3" + "}\n";
HashMap<String,Integer> user=JSONObject.parseObject(test,HashMap.class);
System.out.println(user); // {name=success, age=3}
System.out.println(user.size()); // 2
2、把JSONObject转为简单的实例对象
注意:User类中一定要有含属性的构造函数 ,例如:public User(String name,int age)
String test = "{\n" +
"\t\"name\": \"success\",\n" +
"\t\"age\": 3\t\n" +
"}\n";
User user=JSONObject.parseObject(test,User.class);
System.out.println(user); // {"name":"success", "age" :3}
System.out.println(user.GetAge()); //3
System.out.println(user.GetName()); //success
三、JSONObject相关
1.1 获取JSONObject中的JSONArray
JSONObject json=JSONObject.parseObject(test);
JSONArray dataJson=json.getJSONArray("data");
System.out.println(dataJson);
//输出为:[{"totalCount":11,"errorCount":1},{"totalCount":10,"errorCount":1}]
1.2 获取JSONObject中的JSONArray中的某一部分的JSONObject
JSONObject json=JSONObject.parseObject(test);
JSONArray dataJson=json.getJSONArray("data");
JSONObject json2=dataJson.getJSONObject(0).getJSONObject("test1");
System.out.println(dataJson);
//输出为:[{"test2":{"totalCount":10,"errorCount":1},"test1":{"totalCount":11,"errorCount":1}}]
System.out.println(json2);
//输出为:{"totalCount":11,"errorCount":1}
1.3 获取JSONObject中的 某一String值或int值
JSONObject json=JSONObject.parseObject(test);
JSONArray dataJson=json.getJSONArray("data");
JSONObject json2=dataJson.getJSONObject(0).getJSONObject("test1");
String ss=json2.getString("totalCount");
System.out.println(dataJson);
System.out.println(json2);
System.out.println(ss);
// 11
获取String利用.getString("key"),同理获取Integer利用.getInteger("key"),blalalalalalaa
或者直接使用.get("key").
或者利用JsonPath.read(Object o,"$.status");
1.4 想替换掉JSONObject中某个key对应的value
如果JSONObject层级较深,先利用上面的方法逐层获取到对应Key所在的小JSOnObject串,然后使用put方法
JSONObject json=JSONObject.parseObject(test);
JSONArray dataJson=json.getJSONArray("data");
JSONObject json2=dataJson.getJSONObject(0).getJSONObject("test1");
json2.put("totalCount",0);
1.5 判断JSONObject是否包含某个key或某个value值
JSONObject.containsKey("key"); JSONObject.containsValue("value");