String转成JSON的实现
程序员文章站
2022-03-30 17:45:01
String转成JSON 这个依赖很重要,我们将围绕 中的 这个类来谈转换 1. String转成JSON 一句话就能解决,非常便捷。 想要取出值,可以对 进行操作: 结果为: 2. 将String转为list后转为JSON 3. 将String转为map后转为JSON 特别注意:从JSONObje ......
string转成json
这个依赖很重要,我们将围绕fastjson
中的jsonobject
这个类来谈转换
<dependency> <groupid>com.alibaba</groupid> <artifactid>fastjson</artifactid> <version>1.2.15</version> </dependency>
- string转成json
string json = "{\"abc\":\"1\",\"hahah\":\"2\"}"; jsonobject jsonobject = jsonobject.parseobject(content);
一句话就能解决,非常便捷。 想要取出值,可以对`jsonobject`进行操作:
jsonobject.getstring("abc");
结果为:`1`
- 将string转为list后转为json
list<string> list = new arraylist<string>(); list.add("username"); list.add("age"); list.add("sex"); jsonarray array = new jsonarray(); array.add(list);
- 将string转为map后转为json
map<string, string> map = new hashmap<string, string>(); map.put("abc", "abc"); map.put("def", "efg"); jsonarray array_test = new jsonarray(); array_test.add(map); jsonobject jsonobject = jsonobject.fromobject(map);
特别注意:从jsonobject中取值,碰到了数字为key的时候,如
{ "useranswer": { "28568": { "28552": { "qid": "28552", "order": "1", "userscore": { "score": 100 }, "answer": { "28554": "28554" }, "qscore": "100.0", "qtype": "singlechoice", "sid": "28568" } } }, "papertype": "1", "paperoid": "28567", "instanceid": 30823, "remainingtime": -1, "examoid": "28570" }
获取“useranswer”的value,再转成json,可仿照如下形式:
jsonobject userjson = jsonobject.parseobject(jsonobject.getstring("useranswer"));
但是想获取key"28568"就没这么容易了。直接像上述的写法,会报错。 我们浏览fastjson中的源码,总结下,应该如下写:
jsonobject question = (jsonobject) jsonobject.parseobject(section.getstring("28568"), object.class);
整体代码:
dao代码很容易,就不贴出来了。 package com.xiamenair.training.business.service; import com.alibaba.fastjson.jsonobject; import com.xiamenair.training.business.dao.elearningdao.elearningexaminstancedao; import com.xiamenair.training.business.dao.masterdao.elearningchoicerecorddao; import com.xiamenair.training.business.model.laschoicerecord; import com.xiamenair.training.business.model.entity.elearning.lasexaminstance; import org.springframework.beans.factory.annotation.autowired; import org.springframework.scheduling.annotation.scheduled; import org.springframework.stereotype.service; import java.math.bigdecimal; import java.sql.blob; import java.sql.sqlexception; import java.text.simpledateformat; import java.util.*; @service public class choicerecordservice { //查询数据dao @autowired private elearningchoicerecorddao elearningchoicerecorddao; //转储数据dao @autowired private elearningexaminstancedao elearningexaminstancedao; private choicerecordservice() { } private static class singletonrecordinstance { private static final laschoicerecord choicerecord = new laschoicerecord(); } public static laschoicerecord getmapinstance() { return singletonrecordinstance.choicerecord; } private static simpledateformat simpledateformat = new simpledateformat("yyyy-mm-dd"); /** * 定时任务,每天定时将e学网考试数据分析并转储 * * @param : instanceidlist * @return : void * @author : 28370·皮育才 * @date : 2018/11/20 **/ @scheduled(cron = "00 00 01 * * ?") public void analysischoicerecord() { //获取前一天的时间 date date = new date(); calendar calendar = calendar.getinstance(); calendar.settime(date); calendar.add(calendar.date, -1); date = calendar.gettime(); string datestring = simpledateformat.format(date); list<bigdecimal> instanceidlist = elearningexaminstancedao.findinstanceidbyfinishtime(datestring); if(0 != instanceidlist.size()){ laschoicerecord laschoicerecord = getmapinstance(); instanceidlist.stream().foreach(instanceid -> { blob answerblob = elearningexaminstancedao.finduseranswer(instanceid); long userid = elearningexaminstancedao.finduserid(instanceid); string content = null; try { content = new string(answerblob.getbytes((long) 1, (int) answerblob.length())); } catch (sqlexception e) { e.printstacktrace(); system.out.println("sqlexception:" + e); } jsonobject jsonobject = jsonobject.parseobject(content); //针对本section的"公共"属性直接设置 laschoicerecord.setuserid(userid); laschoicerecord.setpapertype(jsonobject.getstring("papertype")); laschoicerecord.setpaperid(jsonobject.getstring("paperoid")); laschoicerecord.setexaminstanceid(jsonobject.getstring("instanceid")); laschoicerecord.setremainingtime(jsonobject.getstring("remainingtime")); laschoicerecord.setexamid(jsonobject.getstring("examoid")); //针对section中的题目进行细化循环拆分 jsonobject userjson = jsonobject.parseobject(jsonobject.getstring("useranswer")); set sectionset = userjson.keyset(); iterator<string> setit = sectionset.iterator(); analyzeanswer(laschoicerecord, userjson, setit); }); } } private void analyzeanswer(laschoicerecord laschoicerecord, jsonobject userjson, iterator<string> setit) { while (setit.hasnext()) { //对每个question进行再次拆分出题目 jsonobject section = (jsonobject) jsonobject.parseobject(userjson.getstring(setit.next()), object.class); set questionset = section.keyset(); iterator<string> queit = questionset.iterator(); while (queit.hasnext()) { jsonobject question = (jsonobject) jsonobject.parseobject(section.getstring(queit.next()), object.class); string useranswer = question.getstring("answer"); string userscore = question.getstring("userscore"); laschoicerecord.setquestionid(question.getstring("qid")); laschoicerecord.setrecordid(uuid.randomuuid().tostring()); elearningchoicerecorddao.save(laschoicerecord); } } } }
推荐阅读
-
C#实现将Email地址转成图片显示的方法
-
string转jsonarray有双引号(Java把string转json格式的办法)
-
java查看变量类型的方法(将日期从string转成date)
-
Html5中localStorage存储JSON数据并读取JSON数据的实现方法
-
java查看变量类型的方法(将日期从string转成date)
-
Python实现批量把SVG格式转成png、pdf格式的代码分享
-
jsp 判断list是否包含string的实现方法
-
php中输出json对象的值(实现方法)
-
Android开发使用json实现服务器与客户端数据的交互功能示例
-
最简单的JS实现json转csv的方法