首页 车型大全 车型大全示例代码 根据品牌获取所有车型[Java]

根据品牌获取所有车型示例代码[Java]

作者:xiezhongpian 阅读数:1535 上传时间:2017-05-09

根据品牌获取所有车型

package api.jisuapi.car;

import api.util.HttpUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class Carlist {

	public static final String APPKEY = "your_appkey_here";// 你的appkey
	public static final String URL = "https://api.jisuapi.com/car/carlist";
	public static final int parentid = 1;// 品牌ID

	public static void Get() {
		String result = null;
		String url = URL + "?appkey=" + APPKEY + "&parentid=" + parentid;

		try {
			result = HttpUtil.sendGet(url, "utf-8");
			JSONObject json = JSONObject.fromObject(result);
			if (json.getInt("status") != 0) {
				System.out.println(json.getString("msg"));
			} else {
				JSONArray resultarr = json.optJSONArray("result");
				for (int i = 0; i < resultarr.size(); i++) {
					JSONObject obj = (JSONObject) resultarr.opt(i);
					String id = obj.getString("id");
					String name = obj.getString("name");
					String initial = obj.getString("initial");
					String parentid = obj.getString("parentid");
					String depth = obj.getString("depth");
					System.out.println(id + " " + name + " " + initial + " " + parentid + " " + depth);
					if (obj.opt("carlist") != null) {
						JSONArray carlist = obj.optJSONArray("carlist");
						for (int j = 0; j < carlist.size(); j++) {
							JSONObject data = (JSONObject) carlist.opt(j);
							String id1 = data.getString("id");
							String name1 = data.getString("name");
							String fullname = data.getString("fullname");
							String initial1 = data.getString("initial");
							String parentid1 = data.getString("parentid");
							String logo = data.getString("logo");
							String salestate = data.getString("salestate");
							String depth1 = data.getString("depth");
							System.out.println(id1 + " " + name1 + " " + fullname + " " + initial1 + " " + parentid1
									+ " " + logo + " " + salestate + " " + depth1);
							if (data.opt("list") != null) {
								JSONArray list = data.optJSONArray("list");
								for (int x = 0; x < list.size(); x++) {
									JSONObject lists = (JSONObject) list.opt(x);
									String id2 = lists.getString("id");
									String name2 = lists.getString("name");
									String initial2 = lists.getString("initial");
									String parentid2 = lists.getString("parentid");
									String logo1 = lists.getString("logo");
									String price = lists.getString("price");
									String yeartype = lists.getString("yeartype");
									String productionstate = lists.getString("productionstate");
									String salestate1 = lists.getString("salestate");
									String sizetype = lists.getString("sizetype");
									String depth2 = lists.getString("depth");
									System.out.println(id2 + " " + name2 + " " + initial2 + " " + parentid2 + " "
											+ logo1 + " " + price + " " + yeartype + " " + productionstate + " "
											+ salestate1 + " " + sizetype + " " + depth2);
								}
							}
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}