import com.alibaba.fastjson.JSONObject;
//获取token 访问接口获取到一组字符串类型的Json格式数据
String message = wxTokenService.getToken();
//如:{"access_token":"35_lN_99-IrlFS0rFAn2AWVcZ0aDDfOwX1XI3sZwy4F3B8efo87YPMQOXqYHze4TLpyeO5nXR2Ms_-xgcCl-NZ1Pu8xb2LAUq5QELbAFABPW","expires_in":7200}
//字符串转Json
JSONObject jsonObject = JSONObject.parseObject(new String(message));
//获取Json中的数据 根据key获取相应的数据
String access_token = jsonObject.getString("access_token");
List
需要导入的包是
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
JSONArray json = JSONArray.fromObject(字符串类型的JSON);
JSONObject jsonObject = json.getJSONObject(0);
JSONArray aiHumans = jsonObject.getJSONArray("aiHuman");//这里是要获取的Json中的key
Collection<实体类> collection = JSONArray.toCollection(aiHumans, 实体类.class);
for (实体类 别名: collection) {
System.out.println("-----人对象----"+别名);
}
import net.sf.json.JSONObject;
JSONArray json = JSONArray.fromObject(这里放Json);
JSONObject jsonObject = json.getJSONObject(0);
Object o = jsonObject.get("brandId");//get(key的名称)
System.out.println("key对应的值"+o);
import com.alibaba.fastjson.JSON;
Map map=new HashMap();
map.put(key,key);
//map转换为Json
String alarmType= JSON.toJSONString(map);
if (!JSONNull.getInstance().equals(jsonObject.get("aiVehicle")))
{ }
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
JSONArray array = JSONArray.fromObject("JSON数组字符串");
JSONObject jsonObject = JSONObject.fromObject("JSON格式字符串");
格式:
{
"common": [
{
"startTime": "null",
"endTime": "null",
"pmin": "trousers",
"pmax": "null",
"tmin": "null",
"tmax": "null",
"sceneId": "null",
}
]
}
JSONObject jsonObject = JSONObject.fromObject(String s);
Object genderObject = jsonObject.getString("common");
JSONArray jsonArray = JSONArray.fromObject(genderObject.toString());
JSONObject commonObj = JSONObject.fromObject(jsonArray.get(0));
Iterator iter = commonObj.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
}
1、根据key获取值
String str = "{'array':[{'id':1,'name':'张三'},{'id':2,'name':'李四'}]}";
JSONArray jsonArray = null;
jsonArray = jsonobj.getJSONArray("array");
System.out.println(jsonArray.getJSONObject(0).get("name"));
String str = "[{'id':1,'name':'张三'},{'id':2,'name':'李四'}]}";
JSONArray jsonArray = null;
jsonArray = new JSONArray(str);
System.out.println(jsonArray.getJSONObject(0).get("name"));
2、map获取键值
JSONObject json1=JSONObject.fromObject("{'username' : '11111','clientid' : '','password' : '222222'}");
Map map =json1;
for (Entry entry : map.entrySet()) {
System.out.println(entry.getKey()+"="+entry.getValue());
}
3、三提取json中某个数组的所有值
public class JsonExtracter { public static void main(String[] args) { String s = "{\"name\":\"a\",\"family\":[\"张三\",\"李四\"]}";
JSONObject jsonObject = JSON.parseObject(s);
//注意:family中的内容带有中括号[],所以要转化为JSONArray类型的对象
JSONArray family = jsonObject.getJSONArray("family");
for (int i = 0; i < family.size(); i++) { //提取出family中的所有
String s1 = (String) family.get(i);
System.out.println("currentFamily:" + s1);
} }
此处内容来源于:https://www.cnblogs.com/kkxwze/p/11134846.html
JSONObj遍历key
Iterator iterator = jsonObject.keys();
ArrayList keys = new ArrayList();
while (iterator.hasNext())
{
String key = (String) iterator.next();
keys.add(key);
}
判断JSONObj中的key是否存在
if(jsonObject.has(key))
判断JSON数组是否为空
//转换成JSON数组
JSONArray json = JSONArray.fromObject(record.value());
//获取数组的第一个json
JSONObject message = json.getJSONObject(0);
//判断Object中的数组是否为空
if (message.getJSONArray("aiFire")!=null){
JSONArray aiFire = message.getJSONArray("aiFire");
}