首页 菜谱大全 菜谱大全示例代码 菜谱分类[Java]

菜谱分类示例代码[Java]

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

菜谱分类

package api.jisuapi.recipe;

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

public class Class {

	public static final String APPKEY = "your_appkey_here";// 你的appkey
	public static final String URL = "https://api.jisuapi.com/recipe/class";

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

		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 classid = obj.getString("classid");
					String name = obj.getString("name");
					String parentid = obj.getString("parentid");
					System.out.println(classid + " " + name + " " + parentid);
					if (obj.opt("list") != null) {
						JSONArray list = obj.optJSONArray("list");
						for (int j = 0; j < list.size(); j++) {
							JSONObject list_ = (JSONObject) list.opt(j);
							String classid_ = list_.getString("classid");
							String name_ = list_.getString("name");
							String parentid_ = list_.getString("parentid");
							System.out.println(classid_ + " " + name_ + " " + parentid_);
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}