1. 程式人生 > >Json打包與解析

Json打包與解析

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;
 }

}