将获取到的Json存入java对象
2016-08-30 15:30
288 查看
java对象代码
<pre name="code" class="html">public class Hotels {
private String hotelId;
private String updatedTime;
public String getHotelId() {
return hotelId;
}
public void setHotelId(String hotelId) {
this.hotelId = hotelId;
}
public String getUpdatedTime() {
return updatedTime;
}
public void setUpdatedTime(String updatedTime) {
this.updatedTime = updatedTime;
}
}
将获取到json存入java对象
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.ParseException;
import java.util.Date;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class GetJson {
public static final String GET_URL = "http://hdsoutlet.qunar.com/api/hotel/queryHotelList.json";
//将获取到的Json存入java对象
public static void getContent() throws IOException, ParseException{
//123213123
Date epoch = new java.text.SimpleDateFormat("dd/MM/yyyy HH:mm:ss").parse("01/01/1970 08:02:03");
long t=epoch.getTime()/1000;
ReqData r=new ReqData();
r.setAppKey("12345678");
//r.setSign("063cae89a00896187f80eecbf922364a");
r.setSign("75652c627f6c6e448041135be35198c6");
r.setVersion("3.1.0");
r.setSalt(1460968861000L);
// r.setSalt(t);
JSONObject json=new JSONObject();
JSONObject member1 = new JSONObject();
member1.put("appKey", r.getAppKey());
member1.put("salt", r.getSalt());
member1.put("sign", r.getSign());
member1.put("version", r.getVersion());
json.put("head", member1);
//拼凑get请求的字符串
String getURL=GET_URL+"?reqData="+json;
URL getUrl =new URL(getURL);
//根据拼凑的URL打开连接,URL.openConnection函数会根据URL的类型
// 返回不同的URLConnection子类的对象,这里URL是一个http,因此实际返回的是HttpURLConnection
HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();
// 进行连接,但是实际上get request要在下一句的connection.getInputStream()函数中才会真正发到
// 服务器
connection.connect();
//取得输入流,并使用Reader读取
BufferedReader reader=new BufferedReader(new InputStreamReader(connection.getInputStream()));
String lines=reader.readLine();
JSONObject js=JSONObject.fromObject(lines);
String result=js.getString("result");
JSONObject js2=JSONObject.fromObject(result);
JSONArray jsonArray=js2.getJSONArray("hotels");
Hotels[] hotels = new Hotels[jsonArray.size()];
for(int i=0;i<jsonArray.size();i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
hotels[i] = (Hotels) JSONObject.toBean(jsonObject, Hotels.class);
System.out.print("hotelsId:"+hotels[i].getHotelId());
System.out.println("------"+"updatedTime:"+hotels[i].getUpdatedTime());
}
reader.close();
//断开连接
connection.disconnect();
}
}
<pre name="code" class="html">public class Hotels {
private String hotelId;
private String updatedTime;
public String getHotelId() {
return hotelId;
}
public void setHotelId(String hotelId) {
this.hotelId = hotelId;
}
public String getUpdatedTime() {
return updatedTime;
}
public void setUpdatedTime(String updatedTime) {
this.updatedTime = updatedTime;
}
}
将获取到json存入java对象
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.ParseException;
import java.util.Date;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class GetJson {
public static final String GET_URL = "http://hdsoutlet.qunar.com/api/hotel/queryHotelList.json";
//将获取到的Json存入java对象
public static void getContent() throws IOException, ParseException{
//123213123
Date epoch = new java.text.SimpleDateFormat("dd/MM/yyyy HH:mm:ss").parse("01/01/1970 08:02:03");
long t=epoch.getTime()/1000;
ReqData r=new ReqData();
r.setAppKey("12345678");
//r.setSign("063cae89a00896187f80eecbf922364a");
r.setSign("75652c627f6c6e448041135be35198c6");
r.setVersion("3.1.0");
r.setSalt(1460968861000L);
// r.setSalt(t);
JSONObject json=new JSONObject();
JSONObject member1 = new JSONObject();
member1.put("appKey", r.getAppKey());
member1.put("salt", r.getSalt());
member1.put("sign", r.getSign());
member1.put("version", r.getVersion());
json.put("head", member1);
//拼凑get请求的字符串
String getURL=GET_URL+"?reqData="+json;
URL getUrl =new URL(getURL);
//根据拼凑的URL打开连接,URL.openConnection函数会根据URL的类型
// 返回不同的URLConnection子类的对象,这里URL是一个http,因此实际返回的是HttpURLConnection
HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();
// 进行连接,但是实际上get request要在下一句的connection.getInputStream()函数中才会真正发到
// 服务器
connection.connect();
//取得输入流,并使用Reader读取
BufferedReader reader=new BufferedReader(new InputStreamReader(connection.getInputStream()));
String lines=reader.readLine();
JSONObject js=JSONObject.fromObject(lines);
String result=js.getString("result");
JSONObject js2=JSONObject.fromObject(result);
JSONArray jsonArray=js2.getJSONArray("hotels");
Hotels[] hotels = new Hotels[jsonArray.size()];
for(int i=0;i<jsonArray.size();i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
hotels[i] = (Hotels) JSONObject.toBean(jsonObject, Hotels.class);
System.out.print("hotelsId:"+hotels[i].getHotelId());
System.out.println("------"+"updatedTime:"+hotels[i].getUpdatedTime());
}
reader.close();
//断开连接
connection.disconnect();
}
}
相关文章推荐
- JAVA 根据URL获取JSON数据,并解析后存入数据库
- Jquery-根据标签的name属性,获取其value值。存入对象并且转换为Json数组
- java android 泛型类型 获取 json 自动转对象
- java 组装json对象,并获取json对象中的属性
- Java获取json数组对象的实例讲解
- java发起http请求获取返回的Json对象
- java根据url获取json对象
- java将获取到的手机端json串数据转换为java对象
- java根据URL获取json对象
- java发起http请求获取返回的Json对象方法
- 通过JAVA从高德地图URL连接获取json数据 解析并存入数据库的程序举例
- Java获取json数组对象的方法
- java获取本地时间&&jQuery实现ajax&&java封装json对象
- java代码解析json对象获取json对象属性值
- java根据url获取json对象 java根据ip获取地理位置
- GSON(JSON字符串转换成一个相等的Java对象)
- 获取普通Java对象大小
- JSON解析之JSONObject对象---js对象转换成java对象
- Java服务器端List对象转换为JSON对象并返回客户端实例
- 使用JSON-LIB转换JAVA对象