加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

一个简单的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" +
                "}";
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读