精华内容
下载资源
问答
  • 下面小编就为大家分享一篇java的JsonObject对象提取值方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
  • java的JsonObject对象提取值

    万次阅读 2017-05-17 11:43:38
    java的JsonObject对象提取值
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONArray;
    import com.alibaba.fastjson.JSONObject;
    import io.swagger.models.auth.In;
    import java.io.IOException;
    import java.security.KeyManagementException;
    import java.security.NoSuchAlgorithmException;
    import java.util.Iterator;
    
    /**
     * Created by Administrator on 2017/4/21.
     */
    public class test {
    
        public static void main(String[] args) throws Exception {
           
           //第一种格式
    	   /**
    		* [
        		{
            		"data ": [
                		{
                    		"building_id ": "*** ",
                    		"building_num ": "** ",
                    		"door_name ": "** ",
                    		"electric ": "** ",
                    		"room_name ": "** "
                		}
            		],
            		"success ": true
        			}
    			]
    		*/
            String s="[{\"success\":true,\"data\":[{\"building_id\":\"***\",\"building_num\":\"**\",\"room_name\":\"**\",\"door_name\":\"**\",\"electric\":\"**\"}]}]" ;
            String b= s.substring(0,s.length()-1);
            String c=b.substring(1, b.length());
            System.out.println(b+"b___");
            JSONObject jsonx = JSON.parseObject(c);
            JSONArray ja = jsonx.getJSONArray("data");
            for (int i = 0; i < ja.size(); i++) {
                JSONObject jo = ja.getJSONObject(i);
                String building_id = jo.getString("building_id");
                System.out.println(building_id+"building_id>>>>>");
            }
    
           //第二种格式
    	   /**
    		 * [
        			{
            			"password ": "*1234567890 ",
            			"success ": "true "
        			}
    			]
    		 */
            String s="[{\"success\":\"true\",\"password\":\"*1234567890\"}]";
            String b= s.substring(0,s.length()-1);
            String c=b.substring(1, b.length());
            System.out.println(c+"c___");
            JSONObject reagobj = JSONObject.fromObject(c);
            String name = reagobj.getString("password");
            System.out.println(name+"name,,,,,,");
            String password = jm.getString("password");
            System.out.println(password);
            System.out.println("看看有没有值"+password);   
    
           
    		//第三种格式
    		/**
    		* {
        		"data ": {
            		"access_token ": "5a7040ccf66bafd06acd39b6f61c19230eaba426755509646d6da23ddd9fb206 ",
            		"expires_second ": 36000
        		},
        		"rlt_code ": "HH0000 ",
        		"rlt_msg ": "成功 "
    		}
    		 */
            String res="{\"data\":{\"access_token\":\"5a7040ccf66bafd06acd39b6f61c19230eaba426755509646d6da23ddd9fb206\",\"expires_second\":36000},\"rlt_code\":\"HH0000\",\"rlt_msg\":\"成功\"}";
            JSONObject jsonObject= JSON.parseObject(res);
            String data = jsonObject.getString("data");
            JSONObject jsondata= JSON.parseObject(data);
            String token = jsondata.getString("access_token");
    
    		
    		//第四种格式
    		 /**
    		 * { 
    			"data ":
    				{ 
    					"total ":23, 
    					"start ":0, 
    					"total_page ":3, 
    					"rows ":
    						[
    							{ "op_way ": "3 ", "user_mobile ": "15321918571 ", "op_time ":1493881391976, "pwd_no ":30},
    							{ "op_way ": "1 ", "op_time ":1493880995000, "pwd_no ":31}
    						], 
    					"current_page ":1, 
    					"page_size ":10
    				}, 
    			"rlt_code ": "HH0000 ",
    			"rlt_msg ": "成功 "
    		}
    		*/
            String res="{\"data\":{\"total\":23,\"start\":0,\"total_page\":3,\"rows\":[{\"op_way\":\"1\",\"op_time\":1493884964000,\"pwd_no\":31},{\"op_way\":\"3\",\"user_mobile\":\"18518517491\",\"op_time\":1493884615032,\"pwd_no\":30},{\"op_way\":\"3\",\"user_mobile\":\"18518517491\",\"op_time\":1493883836552,\"pwd_no\":30},{\"op_way\":\"1\",\"op_time\":1493883294000,\"pwd_no\":31},{\"op_way\":\"1\",\"op_time\":1493883256000,\"pwd_no\":31},{\"op_way\":\"3\",\"user_mobile\":\"15321918571\",\"op_time\":1493883015371,\"pwd_no\":30},{\"op_way\":\"1\",\"op_time\":1493882007000,\"pwd_no\":31},{\"op_way\":\"3\",\"user_mobile\":\"15321918571\",\"op_time\":1493881498520,\"pwd_no\":30},{\"op_way\":\"3\",\"user_mobile\":\"15321918571\",\"op_time\":1493881391976,\"pwd_no\":30},{\"op_way\":\"1\",\"op_time\":1493880995000,\"pwd_no\":31}],\"current_page\":1,\"page_size\":10},\"rlt_code\":\"HH0000\",\"rlt_msg\":\"成功\"}";
            JSONObject jsonObject= JSON.parseObject(res);
            String data = jsonObject.getString("data");
            JSONObject jsonObjects= JSON.parseObject(data);
            JSONArray ja = jsonObjects.getJSONArray("rows");
            for (int i = 0; i < ja.size(); i++) {
                JSONObject jo = ja.getJSONObject(i);
                String op_way = jo.getString("op_way");
                String op_time = jo.getString("op_time");
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                long lt = new Long(op_time);
                Date date = new Date(lt);
                res = simpleDateFormat.format(date);
                String pwd_no = jo.getString("pwd_no");
                String user_mobile = jo.getString("user_mobile");
                System.out.println(op_way+res+pwd_no+user_mobile+"------------");
            }
    
         
        
        }
    
        }


    展开全文
  • import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import io.swagger.models.auth.In;...import java.io.IOException; import j...
    import com.alibaba.fastjson.JSON;  
    import com.alibaba.fastjson.JSONArray;  
    import com.alibaba.fastjson.JSONObject;  
    import io.swagger.models.auth.In;  
    import java.io.IOException;  
    import java.security.KeyManagementException;  
    import java.security.NoSuchAlgorithmException;  
    import java.util.Iterator;  
      
    /** 
     * Created by Administrator on 2017/4/21. 
     */  
    public class test {  
      
        public static void main(String[] args) throws Exception {  
             
           //第一种格式  
           /** 
            * [ 
                { 
                    "data ": [ 
                        { 
                            "building_id ": "*** ", 
                            "building_num ": "** ", 
                            "door_name ": "** ", 
                            "electric ": "** ", 
                            "room_name ": "** " 
                        } 
                    ], 
                    "success ": true 
                    } 
                ] 
            */  
            String s="[{\"success\":true,\"data\":[{\"building_id\":\"***\",\"building_num\":\"**\",\"room_name\":\"**\",\"door_name\":\"**\",\"electric\":\"**\"}]}]" ;  
            String b= s.substring(0,s.length()-1);  
            String c=b.substring(1, b.length());  
            System.out.println(b+"b___");  
            JSONObject jsonx = JSON.parseObject(c);  
            JSONArray ja = jsonx.getJSONArray("data");  
            for (int i = 0; i < ja.size(); i++) {  
                JSONObject jo = ja.getJSONObject(i);  
                String building_id = jo.getString("building_id");  
                System.out.println(building_id+"building_id>>>>>");  
            }  
      
           //第二种格式  
           /** 
             * [ 
                    { 
                        "password ": "*1234567890 ", 
                        "success ": "true " 
                    } 
                ] 
             */  
            String s="[{\"success\":\"true\",\"password\":\"*1234567890\"}]";  
            String b= s.substring(0,s.length()-1);  
            String c=b.substring(1, b.length());  
            System.out.println(c+"c___");  
            JSONObject reagobj = JSONObject.fromObject(c);  
            String name = reagobj.getString("password");  
            System.out.println(name+"name,,,,,,");  
            String password = jm.getString("password");  
            System.out.println(password);  
            System.out.println("看看有没有值"+password);     
      
             
            //第三种格式  
            /** 
            * { 
                "data ": { 
                    "access_token ": "5a7040ccf66bafd06acd39b6f61c19230eaba426755509646d6da23ddd9fb206 ", 
                    "expires_second ": 36000 
                }, 
                "rlt_code ": "HH0000 ", 
                "rlt_msg ": "成功 " 
            } 
             */  
            String res="{\"data\":{\"access_token\":\"5a7040ccf66bafd06acd39b6f61c19230eaba426755509646d6da23ddd9fb206\",\"expires_second\":36000},\"rlt_code\":\"HH0000\",\"rlt_msg\":\"成功\"}";  
            JSONObject jsonObject= JSON.parseObject(res);  
            String data = jsonObject.getString("data");  
            JSONObject jsondata= JSON.parseObject(data);  
            String token = jsondata.getString("access_token");  
      
              
            //第四种格式  
             /** 
             * {  
                "data ": 
                    {  
                        "total ":23,  
                        "start ":0,  
                        "total_page ":3,  
                        "rows ": 
                            [ 
                                { "op_way ": "3 ", "user_mobile ": "15321918571 ", "op_time ":1493881391976, "pwd_no ":30}, 
                                { "op_way ": "1 ", "op_time ":1493880995000, "pwd_no ":31} 
                            ],  
                        "current_page ":1,  
                        "page_size ":10 
                    },  
                "rlt_code ": "HH0000 ", 
                "rlt_msg ": "成功 " 
            } 
            */  
            String res="{\"data\":{\"total\":23,\"start\":0,\"total_page\":3,\"rows\":[{\"op_way\":\"1\",\"op_time\":1493884964000,\"pwd_no\":31},{\"op_way\":\"3\",\"user_mobile\":\"18518517491\",\"op_time\":1493884615032,\"pwd_no\":30},{\"op_way\":\"3\",\"user_mobile\":\"18518517491\",\"op_time\":1493883836552,\"pwd_no\":30},{\"op_way\":\"1\",\"op_time\":1493883294000,\"pwd_no\":31},{\"op_way\":\"1\",\"op_time\":1493883256000,\"pwd_no\":31},{\"op_way\":\"3\",\"user_mobile\":\"15321918571\",\"op_time\":1493883015371,\"pwd_no\":30},{\"op_way\":\"1\",\"op_time\":1493882007000,\"pwd_no\":31},{\"op_way\":\"3\",\"user_mobile\":\"15321918571\",\"op_time\":1493881498520,\"pwd_no\":30},{\"op_way\":\"3\",\"user_mobile\":\"15321918571\",\"op_time\":1493881391976,\"pwd_no\":30},{\"op_way\":\"1\",\"op_time\":1493880995000,\"pwd_no\":31}],\"current_page\":1,\"page_size\":10},\"rlt_code\":\"HH0000\",\"rlt_msg\":\"成功\"}";  
            JSONObject jsonObject= JSON.parseObject(res);  
            String data = jsonObject.getString("data");  
            JSONObject jsonObjects= JSON.parseObject(data);  
            JSONArray ja = jsonObjects.getJSONArray("rows");  
            for (int i = 0; i < ja.size(); i++) {  
                JSONObject jo = ja.getJSONObject(i);  
                String op_way = jo.getString("op_way");  
                String op_time = jo.getString("op_time");  
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
                long lt = new Long(op_time);  
                Date date = new Date(lt);  
                res = simpleDateFormat.format(date);  
                String pwd_no = jo.getString("pwd_no");  
                String user_mobile = jo.getString("user_mobile");  
                System.out.println(op_way+res+pwd_no+user_mobile+"------------");  
            }  
      
           
          
        }  
      
        }

     

    转载于:https://www.cnblogs.com/Yusco/p/10249318.html

    展开全文
  • 该方法是基于类getter方法 没有getter方法 public class test { private int aga; private String name; private List num; // public List getNum() { // return num; // } // // public int ...

    该方法是基于类的getter方法的

    没有getter方法

    public class test {
        private int aga;
        private String name;
        private List num;
    
    //    public List getNum() {
    //        return num;
    //    }
    //
    //    public int getAga() {
    //        return aga;
    //    }
    
        public void setAga(int aga) {
            this.aga = aga;
        }
    
    //    public String getName() {
    //        return name;
    //    }
    
    //    public void setName(String name) {
    //        this.name = name;
    //    }
    
        public test(int aga, String name,List num) {
            this.aga = aga;
            this.name = name;
            this.num = num;
        }
    
        public static void main(String[] args) {
            List list = new ArrayList();
            list.add("zhang");
            list.add("li");
            test t = new test(27,"lvshenglin",null);
            System.out.println(JSONObject.fromObject(t));  //{}

     

     

    有getter方法

    public class test {
        private int aga;
        private String name;
        private List num;
    
        public List getNum() {
            return num;
        }
    
        public int getAga() {
            return aga;
        }
    
        public void setAga(int aga) {
            this.aga = aga;
        }
    
        public String getName() {
            return name;
        }
    
    //    public void setName(String name) {
    //        this.name = name;
    //    }
    
        public test(int aga, String name,List num) {
            this.aga = aga;
            this.name = name;
            this.num = num;
        }
    
        public static void main(String[] args) {
            List list = new ArrayList();
            list.add("zhang");
            list.add("li");
            test t = new test(27,"lvshenglin",null);
            System.out.println(JSONObject.fromObject(t));  //{"aga":27,"name":"lvshenglin","num":[]}
    展开全文
  • java使用JSONObject实例

    2020-10-26 17:58:52
    JAVAJSONObject对象使用方法
  • JavaJSONObject的使用

    万次阅读 2018-04-17 09:42:48
    Java中JSONObject的使用 JSON(JavaScript Object Notation)是一...在Java中可以通过org.json包中的JSONObject对操作JSON数据 JSON数据格式 Object 使用大括号{}包含的键值对,key必须为String类型,value...

    Java中JSONObject的使用

    JSON(JavaScript Object Notation)是一种与开发语言无关的、轻量级的数据格式。是一种数据格式的规范。具有易读易编写和易解析的优点。在Java中可以通过org.json包中的JSONObject对操作JSON数据

    JSON数据格式

    • Object

      使用大括号{}包含的键值对,key必须为String类型,value为任何基本类型或者数据结构

    • Array

      使用中括号[]括起来,并用逗号分隔

      {
        "name"    : "zp",
        "age" : 12,
        "major"   : ["数据结构", "高等数学"],
        "other"   : {
            "birthday"    : "1990-01-01",
            "like"        : "study"
        }
      }

    导入JSONObject

    org.json包下载地址:http://www.json.org/

    构造JSONObject

    JSONObject();
    JSONObject(String str);
    JSONObject(Object obj);
    JSONObject(Map<String, Object> map);

    JSONObject方法

    // 获取基本数据类型
    E getKeyName(String key);
    // 获取数组
    JSONArray getJSONArray(String key);
    // 获取对象
    JSONObject getJSONObject(String key);
    // 增加属性
    put(String key, Object value);  //会调用value的toString()方法,如果是自定义对象注意重写toString()
    put(String key, JSONObject value);
    // 判断属性为空或者不存在
    boolean isNull(String key);
    // 判断属性存在且不为空
    boolean has(String key);

    例子

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.HashMap;
    import java.util.Map;
    
    import org.json.JSONObject;
    
    public class Main {
        public static void main(String[] args) {
            /**
             * 字符串构建JSON对象
             */
            String str = "{"
                    + "\"name\"     : \"zp\","
                    + "\"age\"  : 12,"
                    + "\"has_g\"    : true,"
                    + "\"major\"    : [\"first\", \"second\"],"
                    + "\"other\"    : {"
                    +    "\"birthday\"  : \"1990-01-01\","
                    +    "\"like\"      : \"study\""
                    +   "};"
                    + "}";
            JSONObject o = new JSONObject(str);
            System.out.println(o.getString("name"));
            System.out.println(o.getInt("age"));
            System.out.println(o.getJSONArray("major"));
            System.out.println(o.getJSONObject("other"));
            /**
             * JavaBean构建JSON对象
             * 传入Object时,会将所有提供get方法的属性生成JSON
             */
            People people = new People();
            people.setId(1);
            people.setName("zp");
            people.setBirthday("1997-1-1");
            people.setAge(16);
            people.love = "134";
            A a = new A();
            a.setType("a123");  // 属性为自定义类不能生成
            people.setA(null);
            JSONObject peopleJson = new JSONObject(people);
            System.out.println(peopleJson.has("a"));
            System.out.println(peopleJson.isNull("a"));
            System.out.println(peopleJson);
            /**
             * 无参构造JSONObject
             */
            JSONObject jsonO = new JSONObject();
            jsonO.put("a", "a");
            jsonO.put("people", people);
            System.out.println(jsonO);
            /**
             * MAP构造JSON
             */
            Map<String, Object> mapStr = new HashMap<String,Object>();
            mapStr.put("type", "jsonMap");
            mapStr.put("arr", new int[]{1,2,3,4,5});
            mapStr.put("obj", people);
            JSONObject jsonMap = new JSONObject(mapStr); 
            System.out.println(jsonMap);
        }
    }
    
    class A {
        private String type;
    
        public String getType() {
            return type;
        }
    
        public void setType(String type) {
            this.type = type;
        }
    
    }
    public class People {
        private int id;
        private String name;
        private String birthday;
        private int age;
        public String love;
        private A a;
        public String getLove() {
            return love;
        }
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getBirthday() {
            return birthday;
        }
        public void setBirthday(String birthday) {
            this.birthday = birthday;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        public A getA() {
            return a;
        }
        public void setA(A a) {
            this.a = a;
        }
        @Override
        public String toString() {
            return "People [id=" + id + ", name=" + name + ", birthday=" + birthday + ", age=" + age + ", love=" + love
                    + ", a=" + a + "]";
        }
    }
    展开全文
  • 主要介绍了Java使用JSONObject操作json,结合实例形式较为详细分析了Java使用JSONObject解析json数据相关原理、使用技巧与操作注意事项,需要朋友可以参考下
  • 主要介绍了JavaJSONObject与JSONArray使用区别详解,小编觉得挺不错,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
  • JSONObject fys = new JSONObject(); fys.put("11","22"); JSONObject fys2 = JSONObject.parseObject(fys.toJSONString()); 将json对象转为字符串,将字符串重新构建 JSONObject
  • java使用JSONobject类所需jar包,包含所需6个jar包, java使用JSONobject类所需jar包,包含所需6个jar包,
  • 主要介绍了JavaJSONObject和JSONArray使用相关资料,需要朋友可以参考下
  • java 获取JSONObject中key对应

    千次阅读 2020-10-12 17:00:03
    java 获取JSONObject中key对应值 下图是一个JSONObject 名字是datas,举例取出description和name值 //取出datas里,data里description JSONObject data = datas.getJSONObject("data"); String ...
  • Java 遍历 JSONObject

    2021-03-31 09:16:55
    fastjson 解析方法 for (Map.Entry<String, Object> entry : jsonObject.entrySet()) { System.out.println("key值="+entry.getKey()); System.out.println("对应key值value="+entry.getValue()); }
  • Java_JSONObject

    2017-06-19 18:17:18
    JSONObject jo=(JSONObject) JSONObject.parse(globalPolicy.getParams()); jo.put("founder", username); 说明: globalPolicy.getParams()是原有数据,这里如果要追加数据,不需要new一个新对象,直接将新...
  • javaJsonObject 使用

    2017-07-24 12:36:49
    JsonObject obj = new JsonParser().parse("{'a':'b','b':'c'}").getAsJsonObject(); String c=obj.get("a").getAsString(); System.out.println(obj); System.out.println(c); 输出结果:(将json中a值,取了...
  • JSONObject与JSONArray 最近在学习过程中用到了稍微复杂点json数据需要将json...JSONObject-lib包是一个beans,collections,maps,java arrays和xml和JSON互相转换包。 2.下载jar包 http://files.cnblogs.com/j...
  • 目录问题解决方法 问题 安卓和JAVA使用JSONObject中getString方法对于同一个JSON中关于整数型提取不同。 比如:{“result”:0} ...基本上可以确定是安卓和JAVAJSONObject实现不一致导致 JAVA中实现:JSONO...
  • 今天给大家介绍JSONObject的是另一种包,全称是这样: org.springframework.boot.configurationprocessor.json.JSONObject pom导入包是: <dependency> <groupId>org.springframework.boot</...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 4,184
精华内容 1,673
关键字:

java的jsonobject

java 订阅