public class FastJsonUtils {
private static final SerializeConfig config;
static {
config = new SerializeConfig();
config.put(java.util.Date.class, new JSONLibDataFormatSerializer());
config.put(java.sql.Date.class, new JSONLibDataFormatSerializer());
}
private static final SerializerFeature[] features = {
SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteNullListAsEmpty,
SerializerFeature.WriteNullNumberAsZero,
SerializerFeature.WriteNullBooleanAsFalse,
SerializerFeature.WriteNullStringAsEmpty,
SerializerFeature.WriteDateUseDateFormat
};
public static String toJSONString(Object object) {
return JSON.toJSONString(object, config, features);
}
public static String toJSONString1(Object object) { return JSON.toJSONString(object, new PascalNameFilter()); }
public static String toJSONString(Object object,String dateFormat) {
return JSON.toJSONStringWithDateFormat(object, dateFormat, features);
}
public static String toJSONNoFeatures(Object object) {
return JSON.toJSONString(object, config);
}
public static String toJSONNoConfig(Object object){
return JSON.toJSONString(object,features);
}
public static Object toBean(String text) {
return JSON.parse(text);
}
public static <T> T toBean(String text, Class<T> clazz) {
return JSON.parseObject(text, clazz);
}
public static <T> Object[] toArray(String text) {
return toArray(text, null);
}
public static <T> Object[] toArray(String text, Class<T> clazz) {
return JSON.parseArray(text, clazz).toArray();
}
public static <T> List<T> toList(String text, Class<T> clazz) {
return JSON.parseArray(text, clazz);
}
public static Object beanToJson(KeyValue keyvalue) {
String textJson = JSON.toJSONString(keyvalue);
Object objectJson = JSON.parse(textJson);
return objectJson;
}
public static Object textToJson(String text) {
Object objectJson = JSON.parse(text);
return objectJson;
}
public static Map stringToCollect(String s) {
Map m = JSONObject.parseObject(s);
return m;
}
public static String collectToString(Map m) {
String s = JSONObject.toJSONString(m);
return s;
}
public static String listmapToString(List<Map<String, Object>> lm) {
String s = JSONObject.toJSONString(lm);
return s;
}
public static String obj2Json(Object obj)
{
String str = JSON.toJSONString(obj, config);
return str;
}
@SuppressWarnings("unchecked")
public static Map<String,Object> json2Map(String json){
return JSON.parseObject(json, Map.class);
}
@SuppressWarnings("rawtypes")
public static List<HashMap> json2List(String json){
return JSON.parseArray(json, HashMap.class);
}
public static <T>T objectToBean(Object o, Class c){
return JSON.parseObject(JSON.toJSONString(o), (Type) c);
}
public static String readJsonFile() {
String jsonStr = "";
try {
File jsonFile = new File("C:\Users\Administrator\Desktop\test.json");
FileReader fileReader = new FileReader(jsonFile);
Reader reader = new InputStreamReader(new FileInputStream(jsonFile),"utf-8");
int ch = 0;
StringBuffer sb = new StringBuffer();
while ((ch = reader.read()) != -1) {
sb.append((char) ch);
}
fileReader.close();
reader.close();
jsonStr = sb.toString();
return jsonStr;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public static void main(String[] args) {
String s = readJsonFile();
JSONArray movies = JSON.parseArray(s);
for (int i = 0 ; i < movies.size();i++){
JSONObject key = (JSONObject)movies.get(i);
JSONArray movies1 = JSON.parseArray(String.valueOf(key.get("courseChapterSections")));
jsonTestAdd(movies1);
}
}
public static void jsonTestAdd(JSONArray movies1){
for (int j = 0 ; j < movies1.size();j++){
JSONObject key1 = (JSONObject)movies1.get(j);
JSONArray movies2 = JSON.parseArray(String.valueOf(key1.get("courseChapterSections")));
if(movies2!=null && movies2.size()>0){
jsonTestAdd(movies2);
}else{
JSONObject progress1 =JSON.parseObject(String.valueOf(key1.get("progress")));
if(progress1!=null && progress1.size()>0) {
String finishStatus=String.valueOf(progress1.get("finishStatus"));
if(finishStatus!=null && StringUtils.isNotBlank(finishStatus) &&finishStatus!="null"){
if(!"2".equals(finishStatus)){
System.out.println("'"+progress1.get("resourceId")+"',");
}
}
}
}
}
}
}