调查问卷的JSON模板设计与分数计算的Java实现

首先设计问卷调查模板如下:

{
    "吸烟史问卷调查":
    [
        {
            "序号":"1",
            "问题":"吸烟吗",
            "选项": 
            [
                {
                    "序号":"1",
                    "答案":"不吸",
                    "得分":"90"
                },
                {
                    "序号":"2",
                    "答案":"吸烟",
                    "得分":"0-60",
                    "选项": 
                    [
                        {
                            "序号":"1",
                            "问题":"频率",
                            "选项": 
                            [
                                {
                                    "序号":"1",
                                    "答案":"每天",
                                    "得分":"0"
                                },
                                {
                                    "序号":"2",
                                    "答案":"周1-2次",
                                    "得分":"10"
                                },
                                {
                                    "序号":"3",
                                    "答案":"月1-2次",
                                    "得分":"15"
                                },
                                {
                                    "序号":"4",
                                    "答案":"极少",
                                    "得分":"20"
                                }
                            ]
                        },
                        {
                            "序号":"2",
                            "问题":"哪一类",
                            "选项": 
                            [
                                {
                                    "序号":"1",
                                    "答案":"过滤嘴", 
                                    "得分":"15"
                                },  
                                {   
                                    "序号":"2",
                                    "答案":"雪茄", 
                                    "得分":"10"
                                },  
                                {   
                                    "序号":"3",
                                    "答案":"叶子烟", 
                                    "得分":"5"
                                },  
                                {   
                                    "序号":"4",
                                    "答案":"烟斗烟袋", 
                                    "得分":"0"
                                }
                            ]
                        },
                        {
                            "序号":"3",
                            "问题":"平均每天吸多少",
                            "选项": 
                            [
                                {
                                    "序号":"1",
                                    "答案":"小于10", 
                                    "得分":"10"
                                },  
                                {   
                                    "序号":"2",
                                    "答案":"10-20", 
                                    "得分":"7"
                                },  
                                {   
                                    "序号":"3",
                                    "答案":"20-30", 
                                    "得分":"4"
                                },  
                                {   
                                    "序号":"4",
                                    "答案":"30以上", 
                                    "得分":"0"
                                }
                            ]
                        },
                        {
                            "序号":"4",
                            "问题":"多少年",
                            "选项": 
                            [
                                {
                                    "序号":"1",
                                    "答案":"0-2年", 
                                    "得分":"15"
                                },  
                                {   
                                    "序号":"2",
                                    "答案":"2-4年", 
                                    "得分":"10"
                                },  
                                {   
                                    "序号":"3",
                                    "答案":"4-8年", 
                                    "得分":"5"
                                },  
                                {   
                                    "序号":"4",
                                    "答案":"8年以上", 
                                    "得分":"0"
                                }
                            ]
                        }
                    ]
                },
                {
                    "序号":"3",
                    "答案":"曾经吸",
                    "得分":"61-89",
                    "选项": 
                    [
                        {
                            "序号":"1",
                            "问题":"平均每天吸多少",
                            "选项": 
                            [
                                {
                                    "序号":"1",
                                    "答案":"小于10",
                                    "得分":"45"
                                },
                                {
                                    "序号":"2",
                                    "答案":"10-20",
                                    "得分":"40"
                                },
                                {
                                    "序号":"3",
                                    "答案":"20-30",
                                    "得分":"35"
                                },
                                {
                                    "序号":"4",
                                    "答案":"30以上",
                                    "得分":"30"
                                }
                            ]
                        },
                        {
                            "序号":"2",
                            "问题":"戒烟多少岁减去多少岁开始",
                            "选项": 
                            [
                                {
                                    "序号":"1",
                                    "答案":"0-2年",
                                    "得分":"44"
                                },
                                {
                                    "序号":"2",
                                    "答案":"2-4年",
                                    "得分":"40"
                                },
                                {
                                    "序号":"3",
                                    "答案":"4-8年",
                                    "得分":"35"
                                },
                                {
                                    "序号":"4",
                                    "答案":"8年以上",
                                    "得分":"31"
                                }
                            ]
                        }
                    ]
                }
            ]
        },
        {
            "序号":"2",
            "问题":"被动吸烟",
            "选项": 
            [
                {
                    "序号":"1",
                    "答案":"是", 
                    "得分":"0"
                },
                {
                    "序号":"2",
                    "答案":"否",
                    "得分":"10"
                }
            ]
        }
    ]
}

用户完成问卷调查后,获得答案如下:

{
    "吸烟史问卷调查":
    [
        {
            "序号":"1",
            "选项": 
            [
                {
                    "序号":"2",
                    "选项": 
                    [
                        {
                            "序号":"1",
                            "选项": 
                            [
                                {
                                    "序号":"1"
                                }
                            ]
                        },
                        {
                            "序号":"2",
                            "选项": 
                            [
                                {
                                    "序号":"1"
                                }
                            ]
                        },
                        {
                            "序号":"3",
                            "选项": 
                            [
                                {
                                    "序号":"1"
                                }
                            ]
                        },
                        {
                            "序号":"4",
                            "选项": 
                            [
                                {
                                    "序号":"1"
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}

创建解析及计算类如下:

package cal;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Set;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class Calculator {
	private int scoreU;
	private int scoreL;

	public Calculator() {
		super();
		this.scoreU = 0;
		this.scoreL = 0;
	}
	
	public String getScore() {
		if (this.scoreU !=this.scoreL) {
			return this.scoreL + "-" + this.scoreU;
		} else {
			return this.scoreU + "";
		}
	}
	
	public void drillDown(JSONObject _jsonObjectA, JSONObject _jsonObjectQ) {
		Set keys = _jsonObjectA.keySet();
		for (Object key:keys) {
			Object objectA = _jsonObjectA.get(key);
			Object objectQ = _jsonObjectQ.get(key);
			
			if (objectA instanceof JSONObject) {
				drillDown((JSONObject)objectA, (JSONObject)objectQ);
			} else if (objectA instanceof JSONArray) {
				JSONArray arrayA = (JSONArray)objectA;
				JSONArray arrayQ = (JSONArray)objectQ;
				for (Object objectA1:arrayA) {
					if (objectA1 instanceof JSONObject) {
						Object objectQ1 = null;
						for (Object objectQT:arrayQ) {
							if (((JSONObject)objectA1).get("序号").equals(((JSONObject)objectQT).get("序号"))) {
								objectQ1 = objectQT;
							}
						}
						drillDown((JSONObject)objectA1, (JSONObject)objectQ1);
					}
				}
			} else if (objectA instanceof String) {
				String score = (String)_jsonObjectQ.get("得分");
				if (null != score) {
					int index = score.indexOf("-");
					if (-1 < index) {
						this.scoreL = this.scoreL + Integer.parseInt(score.substring(0, index));
						this.scoreU = this.scoreU + Integer.parseInt(score.substring(index+1));
					} else {
						this.scoreL = this.scoreL + Integer.parseInt(score);
						this.scoreU = this.scoreU + Integer.parseInt(score);
					}
				}
			}
		}
	}
	
	public void calculate() {
		try {
			InputStreamReader inputStreamReaderQ = new InputStreamReader(new FileInputStream("D:\\MySession\\2020\\20201202\\吸烟史问卷调查.JSON"), "UTF-8");
			BufferedReader bufferedReaderQ = new BufferedReader(inputStreamReaderQ);
			String lineQ = null;
			StringBuffer linesQ = new StringBuffer();
			while(null != (lineQ = bufferedReaderQ.readLine())) {
				linesQ.append(lineQ);
			}
			bufferedReaderQ.close();
			JSONObject jsonObjectQ = JSONObject.fromObject(linesQ.toString());
			
			InputStreamReader inputStreamReaderA = new InputStreamReader(new FileInputStream("D:\\MySession\\2020\\20201202\\吸烟史问卷调查用户回答.JSON"), "UTF-8");
			BufferedReader bufferedReaderA = new BufferedReader(inputStreamReaderA);
			String lineA = null;
			StringBuffer linesA = new StringBuffer();
			while(null != (lineA = bufferedReaderA.readLine())) {
				linesA.append(lineA);
			}
			bufferedReaderA.close();
			JSONObject jsonObjectA = JSONObject.fromObject(linesA.toString());

			drillDown(jsonObjectA, jsonObjectQ);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		Calculator calculator = new Calculator();
		calculator.calculate();
		System.out.println(calculator.getScore());
	}
}

运行结果如下:

40-100

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值