JSONObject转换,遍历,字符串转换JSON,获取Json数据,JSON转实体类对象

 

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>list=new ArrayList();
//遍历Json 获取Json中的数据
//获取某个key下的值
        JSONArray object = JSONObject.parseObject(liveRoom).getJSONArray("room_info");
        //获取数据
        if (object.size()>0){
            //遍历
            for (int i = 0; i < object.size(); i++) {
                Map map=new HashMap<>();
                JSONObject jsonObject1 = object.getJSONObject(i);
                //遍历获取键和值
                Iterator iter = jsonObject1.entrySet().iterator();
                while (iter.hasNext()) {
                    Map.Entry entry = (Map.Entry) iter.next();
                    map.put(entry.getKey().toString(),entry.getValue().toString());
                }
                list.add(map);
            }
        }

//获取Json的方式
     JSONObject jsonObject = JSONObject.fromObject(a);
     Object genderObject = jsonObject.getString("gender");
     JSONObject gender1 = JSONObject.fromObject(genderObject);

 JSON转实体类对象

       需要导入的包是
        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("-----人对象----"+别名);
        }

 获取JSON中的值

        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);

Map转成JSON

import com.alibaba.fastjson.JSON;

Map map=new HashMap();
 map.put(key,key);
 //map转换为Json
String  alarmType= JSON.toJSONString(map);

解决JSONObject["aiVehicle"] is not a JSONArray. 异常

 if (!JSONNull.getInstance().equals(jsonObject.get("aiVehicle")))
{  }

字符串转JSON

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
 
JSONArray array = JSONArray.fromObject("JSON数组字符串");
JSONObject jsonObject = JSONObject.fromObject("JSON格式字符串");

遍历JsonObject对象

格式:
{
  "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();
        }

java获取json数组格式中的值

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");
        }

 

你可能感兴趣的:(工具,java)