一个简单的fastJson实例
发布时间:2020-12-16 18:49:51 所属栏目:百科 来源:网络整理
导读:import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONArray;import com.alibaba.fastjson.JSONObject;import com.td.bo.core.masterdata.entity.syssetting.appEntity.AppQuestion;import com.td.bo.core.masterdata.entity.syssetting.appEn
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.td.bo.core.masterdata.entity.syssetting.appEntity.AppQuestion;
import com.td.bo.core.masterdata.entity.syssetting.appEntity.AppQuestionItem;
import com.td.masterdata.entity.application.App;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
/**
* Created by Jerry on 2017/1/16.
* God Bless Me
*/
public class TestJson {
public static void main(String[] args) {
JSONObject json = (JSONObject) JSON.parse(getJsonValue());
JSONObject jsonSub = json.getJSONObject("subsystemAccept");
JSONObject jsonProperty = json.getJSONObject("propertyAccept");
JSONObject jsonOver = json.getJSONObject("overallAccept");
// saveQues(jsonSub);
// saveQues(jsonProperty);
saveQues(jsonOver);
}
public static void saveQues(JSONObject json) {
Set<String> set = json.keySet();
for (String key : set) {
System.out.println("key==" + key);
JSONArray list = json.getJSONArray(key);
for (Object obj : list) {
JSONObject jsonObject = (JSONObject) obj;
//问题
AppQuestion appQuestion = JSON.toJavaObject((JSON) jsonObject.get("question"),AppQuestion.class);
//答案
String answer = jsonObject.getString("answer");
appQuestion.setAnswer(answer);
//选项
List<AppQuestionItem> questionItems = new ArrayList<>();
JSONArray selectItemArray = jsonObject.getJSONArray("selectItems");
for (Object o : selectItemArray) {
AppQuestionItem questionItem = new AppQuestionItem();
questionItem.setSelectContent(o.toString());
questionItems.add(questionItem);
}
System.out.println("保存成功");
}
}
}
public static String getJsonValue() {
return "{n" +
" "subsystemAccept": {n" +
" "sub_1": [n" +
" {n" +
" "question": {n" +
" "code": "good",n" +
" "quesTypeCode": "radio_type",n" +
" "quesTitle": "这是子系统第一个问题",n" +
" "refType": "accept",n" +
" "categoryCode": "subsystemAccept",n" +
" "id": 285,n" +
" "refId": 28,n" +
" "groupCode": "sub_1",n" +
" "status": 1n" +
" },n" +
" "answer": "否",n" +
" "selectItems": [n" +
" "是",n" +
" "否"n" +
" ],n" +
" "valueItems": [n" +
" {n" +
" "name": "是",n" +
" "value": "是"n" +
" },n" +
" {n" +
" "name": "否",n" +
" "value": "否"n" +
" }n" +
" ]n" +
" },n" +
"ttt {n" +
" "question": {n" +
" "code": "good",n" +
" "quesTitle": "这是子系统第二个问题",n" +
" ],n" +
"tt"sub_2": [n" +
" {n" +
" "question": {n" +
" "code": "good",n" +
" "quesTitle": "位置好",n" +
" "value": "否"n" +
" }n" +
" ]n" +
" }n" +
" ]n" +
" },n" +
" "acceptTpl": {n" +
" "code": "5code",n" +
" "createTime": 1484537601000,n" +
" "name": "验收5xiu",n" +
" "updateTime": 1484537601000,n" +
" "id": 28,n" +
" "state": 1,n" +
" "businessType": "XNY",n" +
" "projectId": 87n" +
" },n" +
" "propertyAccept": {n" +
" "001": [n" +
" {n" +
" "question": {n" +
" "code": "heighh",n" +
" "quesTitle": "别墅高",n" +
" "categoryCode": "propertyAccept",n" +
" "id": 283,n" +
" "groupCode": "001",n" +
" "answer": "是",n" +
" {n" +
" "question": {n" +
" "unit": "",n" +
" "code": "width",n" +
" "quesTitle": "宽",n" +
" "id": 284,n" +
" "overallAccept": {}n" +
"}";
}
}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |