欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java的JsonObject对象提取值方法

程序员文章站 2023-11-27 11:47:58
实例如下所示: import com.alibaba.fastjson.json; import com.alibaba.fastjson.jsonarray;...

实例如下所示:

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

以上这篇java的jsonobject对象提取值方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。