package com.example.global;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class JsonData {
public JsonData() {
}
public final static int DESK_ID = 8; //桌号
public final static int MENU_MAINFOOD = 0; // 特色菜
public final static int MENU_SPECAIL = 1; // 主食
public final static int MENU_PEICAI = 2; // 配菜
public final static int MENU_JIUSHUI = 3; // 酒水
public static String GLOBAL_MENU = null; // 全局菜单
private static List<Map<String, Object>> MENU_LIST ;
private static JSONObject GLOBA_JSON_ORDER;//创建json数据对象
/*
* 解析并打包多个数据的Json,用投射到listview
* 同时筛选可用信息
*/
public static void parseJsonMulti(String strMenu, int menuType) {
try {
JSONArray jsonObjs = new JSONObject(strMenu)
.getJSONArray("carte");
System.out.println("Jsons Working!!!!!");
MENU_LIST = new ArrayList<Map<String, Object>>();
for (int i = 0; i < jsonObjs.length(); i++) {
System.out.println("jsonObjs.length()"+jsonObjs.length());
JSONObject jsonObj = (JSONObject) jsonObjs.getJSONObject(i);
int type = Integer.parseInt(jsonObj.getString("type"));
System.out.println("type = "+type);
if(type == menuType){ //取菜名,单价放到list去
String name = jsonObj.getString("dishname");
String price = jsonObj.getString("price");
String id = jsonObj.getString("dishid");
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", name);
map.put("price", price);
map.put("num", "0");
map.put("dishid", id);
MENU_LIST.add(map); //添加到容器
}
}
} catch (JSONException e) {
System.out.println("Jsons parse error !");
e.printStackTrace();
}catch (NullPointerException e){
}
}
/*
* 打包订单发个服务器
* 内容包括:入座桌号,订单号(设为"0"),num,dishid,
* 和总数
* */
public static void packOrder(){
GLOBA_JSON_ORDER = new JSONObject();//创建json数据对象
try {
GLOBA_JSON_ORDER.put("orderid","0");
//orderjson.put("uid", 123);
GLOBA_JSON_ORDER.put("deskid", JsonData.DESK_ID);
JSONArray carte = new JSONArray();//创建json数组
for(int i=0; i<GlobalOrder.getMYORDER().size(); i++){
JSONObject temp = new JSONObject();
temp.put("id", GlobalOrder.getMYORDER().get(i).get("dishid"));
temp.put("number", GlobalOrder.getMYORDER().get(i).get("num"));
carte.put(temp);
}
GLOBA_JSON_ORDER.put("carte", carte);
GLOBA_JSON_ORDER.put("total number", GlobalOrder.getMYORDER().size());
System.out.println("发送订单: o"+GLOBA_JSON_ORDER.toString());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*
* 分析接收到催单和结账指令,后,后台反馈的信息
* 成功return true
* 失败return false
* */
public static boolean parseJsonUrge(String msg){
try {
JSONObject json = new JSONObject(msg);
System.out.println("json.get()---->"+json.get("result"));
if(json.get("result").equals("1"))
return true;
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
public static List<Map<String, Object>> getMENU_LIST() {
return MENU_LIST;
}
public static void setGLOBAL_MENU(String gLOBAL_MENU) {
GLOBAL_MENU = gLOBAL_MENU;
System.out.println("___setGLOBAL_MENU:"+GLOBAL_MENU);
}
public static JSONObject getGLOBA_JSON_ORDER() {
return GLOBA_JSON_ORDER;
}
}