定义java实体类
实体类对应于必须在json数据中检索的json对象
公共类参数实体{
私有字符串id;
私有字符串名称;
公共字符串获取
返回id;
}
公共void setid (stringid ) {
this.id=id;
}
公共字符串获取名称
return name;
}
公共void set name (string name ) {
this.name=name;
}
}
2 .从URL中获取json对象,将其转换为java对象,然后返回java对象List
公共类测试1 {
//取得具体的url,解析json对象数据
公共字符串规格(string URL )。
Listparamaters=new ArrayList (;
try {
stringbuilder JSON=new stringbuilder (;
//连接url
URLURLobject=newURL(URL;
urlconnectionuc=URL object.open connection (;
httpurlconnectionhttpurlconnection=(httpurl connection ) uc;
//伪造浏览器请求
httpurl connection.setrequestproperty (accept )、application/json、text/javascript、*/*; q=0.01 ';
httpurl connection.setrequestproperty (' accept-encoding ',' gzip,deflate ' );
httpurl connection.setrequestproperty (' accept-language ',' zh-CN,zh; q=0.8,zh-TW; q=0.7,zh-HK; q=0.5,en-US; q=0.3,en; q=0.2 ';
httpurl connection.setrequestproperty (' connection ',' keep-alive ' );
httpurl connection.setrequestproperty (' content-type ',' application/x-www-form-urlencoded ' );
httpurl connection.setrequestproperty (' user-agent ',' Mozilla/5.0 ) windowsnt10.0; Win64; x64; rv:75.0 ) Gecko/20100101 Firefox/75.0 );
//读取从该url返回的json数据
bufferedreaderin=newbufferedreader (newinputstreamreader (httpurl connection.get inputstream ),' utf-8 ' )
string inputline=空;
while((inputline=in.readline ) )!=null ) {
JSON.append(inputline;
}
//关闭输入流
in.close (;
将json数据转换为java数据进行处理
jsonobjectjsonobjects=JSON object.from object (JSON.tostring () );//从//java到json
jsonarrayjsonarray=JSON objects.getjsonarray (' rows ); //其中' rows '是json对象中的数组,该数组还包含一组对象
//遍历此json对象数组
for(intj=0; j