参考:技术参考-大幸运的博客技术站
1,
String output = (url);
(output);
JSONArray jsonArray = (output);
return jsonArray;
2,String转成JSON的实现_javaQQ561487941的博客-****博客_string转成json
String转成JSON
String json = "{"abc":"1","hahah":"2"}";
JSONObject jsonObject = (content);
一句话就能解决,非常便捷。
想要取出值,可以对`jsonObject`进行操作:
("abc");
结果为:`1`
将String转为list后转为JSON
List<String> list = new ArrayList<String>();
("username");
("age");
("sex");
JSONArray array = new JSONArray();
(list);
将String转为map后转为JSON
Map<String, String> map = new HashMap<String, String>();
("abc", "abc");
("def", "efg");
JSONArray array_test = new JSONArray();
array_test.add(map);
JSONObject jsonObject = (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 = (("userAnswer"));
但是想获取key"28568"就没这么容易了。直接像上述的写法,会报错。
我们浏览fastjson中的源码,总结下,应该如下写:
JSONObject question = (JSONObject) (("28568"), );
整体代码:
dao代码很容易,就不贴出来了。
package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import .*;
@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 ;
}
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 = ();
(date);
(, -1);
date = ();
String dateString = (date);
List<BigDecimal> instanceIdList = (dateString);
if(0 != ()){
LasChoiceRecord lasChoiceRecord = getMapInstance();
().forEach(instanceId -> {
Blob answerBlob = (instanceId);
Long userId = (instanceId);
String content = null;
try {
content = new String(((long) 1, (int) ()));
} catch (SQLException e) {
();
("SQLEXCEPTION:" + e);
}
JSONObject jsonObject = (content);
//针对本section的"公共"属性直接设置
(userId);
(("paperType"));
(("paperOid"));
(("instanceId"));
(("remainingTime"));
(("examOid"));
//针对section中的题目进行细化循环拆分
JSONObject userJson = (("userAnswer"));
Set sectionSet = ();
Iterator<String> setIt = ();
analyzeAnswer(lasChoiceRecord, userJson, setIt);
});
}
}
private void analyzeAnswer(LasChoiceRecord lasChoiceRecord, JSONObject userJson, Iterator<String> setIt) {
while (()) {
//对每个question进行再次拆分出题目
JSONObject section = (JSONObject) ((()), );
Set questionSet = ();
Iterator<String> queIt = ();
while (()) {
JSONObject question = (JSONObject) ((()), );
String userAnswer = ("answer");
String userScore = ("userScore");
(("qId"));
(().toString());
(lasChoiceRecord);
}
}
}
}
---------------------
作者:JavaQQ群854630135
来源:****
原文:/javaQQ561487941/article/details/84328669
版权声明:本文为博主原创文章,转载请附上博文链接!
不知道为啥
String json = "{"abc":"1","hahah":"2"}";
JSONObject jsonObject = (content);
会一直抱错, object这个好像不行
7-1
今天遇到的这个问题,找了很多办法
最后这样解决的
src :
[{"comcode":"2143214","licenseno":"粤·13","qqq":"2131233123","startdate":"2019-01-01"},
{"comcode":"124124","licenseno":"粤·1338","qqqq":"1231233123123","startdate":"2019-01-01"}]
JSONArray ja = (src); //把字符串转成json数组
String one = (0); //取第一个元素
JSONObject jb123 = (one); //把这一个json串转成 json对象
("licenseno is --------------- + ("licenseno")); //获取属性值
一开始写的是 把src直接传进到 parseObject 直接报错
cannot be cast to
debug进去 JSONObject 方法,慢慢发现,只能传一个{sadasdasd}这样的json格式的字段,不能传多个,[] 也不能有,尝试把src 的【】用replace 去掉,也不能去掉中间那个逗号
所以最后思路
把字符串转成 json格式的数据,然后再取了再传进去变成jsonobject
这样就能获取到一个json对象的对应属性 元素
所以啊 要多用 debug 多看底层源码 搞懂为啥