java-string转成json 1,
String output = FileUtils.ReadFile(url);
System.out.println(output);
JSONArray jsonArray =  JSONObject.parseArray(output);
return jsonArray;
2,
String转成JSON
String json = "{"abc":"1","hahah":"2"}";
JSONObject jsonObject = JSONObject.parseObject(content);
⼀句话就能解决,⾮常便捷。
想要取出值,可以对`jsonObject`进⾏操作:
结果为:`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.String("userAnswer"));
但是想获取key"28568"就没这么容易了。直接像上述的写法,会报错。
我们浏览fastjson中的源码,总结下,应该如下写:
JSONObject question = (JSONObject) JSONObject.String("28568"), Object.class);整体代码:
dao代码很容易,就不贴出来了。
package aining.business.service;
import com.alibaba.fastjson.JSONObject;
import aining.business.dao.elearningdao.ELearningExamInstanceDao;
import aining.business.dao.masterdao.ELearningChoiceRecordDao;
import del.LasChoiceRecord;
import ity.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;
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 = Instance();
calendar.setTime(date);
calendar.add(calendar.DATE, -1);
date = Time();
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 Bytes((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.String("paperType"));
lasChoiceRecord.String("paperOid"));
lasChoiceRecord.String("instanceId"));
lasChoiceRecord.String("remainingTime"));
lasChoiceRecord.String("examOid"));
//针对section中的题⽬进⾏细化循环拆分
JSONObject userJson = JSONObject.String("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()) {
while (setIt.hasNext()) {
//对每个question进⾏再次拆分出题⽬
JSONObject section = (JSONObject) JSONObject.()), Object.class);
Set questionSet = section.keySet();
Iterator<String> queIt = questionSet.iterator();
while (queIt.hasNext()) {
JSONObject question = (JSONObject) JSONObject.()), Object.class);
String userAnswer = String("answer");
String userScore = String("userScore");
lasChoiceRecord.String("qId"));
lasChoiceRecord.setRecordId(UUID.randomUUID().toString());
eLearningChoiceRecordDao.save(lasChoiceRecord);
}
}
}
}
string转date的方法---------------------
作者:JavaQQ854630135
来源:CSDN
原⽂:blog.csdn/javaQQ561487941/article/details/84328669
版权声明:本⽂为博主原创⽂章,转载请附上博⽂链接!
不知道为啥
String json = "{"abc":"1","hahah":"2"}";
JSONObject jsonObject = JSONObject.parseObject(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 = JSONArray.parseArray(src);  //把字符串转成json数组
String one = ja.getString(0);  //取第⼀个元素
JSONObject jb123 = JSONObject.parseObject(one);    //把这⼀个json串转成 json对象
System.out.println("licenseno is --------------- + ("licenseno"));    //获取属性值
⼀开始写的是  把src直接传进到  parseObject    直接报错
com.alibaba.fastjson.JSONArray cannot be cast to com.alibaba.fastjson.JSONObject
debug进去 JSONObject  ⽅法,慢慢发现,只能传⼀个{sadasdasd}这样的json格式的字段,不能传多个,[] 也不能有,尝试把src 的【】⽤replace 去掉,也不能去掉中间那个逗号
所以最后思路
把字符串转成 json格式的数据,然后再取了再传进去变成jsonobject
这样就能获取到⼀个json对象的对应属性 元素
所以啊 要多⽤ debug  多看底层源码  搞懂为啥

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。