前端之家收集整理的这篇文章主要介绍了
一个简单的fastJson实例,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
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\": 1\n" +
" },\n" +
" \"answer\": \"否\",\n" +
" \"selectItems\": [\n" +
" \"是\",\n" +
" \"否\"\n" +
" ],\n" +
" \"valueItems\": [\n" +
" {\n" +
" \"name\": \"是\",\n" +
" \"value\": \"是\"\n" +
" },\n" +
" {\n" +
" \"name\": \"否\",\n" +
" \"value\": \"否\"\n" +
" }\n" +
" ]\n" +
" },\n" +
"\t\t\t {\n" +
" \"question\": {\n" +
" \"code\": \"good\",\n" +
" \"quesTitle\": \"这是子系统第二个问题\",\n" +
" ],\n" +
"\t\t\"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\": 87\n" +
" },\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" +
"}";
}
}