Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.ArrayList;
import java.util.Iterator;
public class Solution {
public static void main(String[] args) {
Solution solution = new Solution();
String a = "{\"enter\":\"hhhhhh\",\"shoplist\":[{\"basic\":{\"name\":null,\"id\":null,\"addr\":null},\"service\":{\"aftersale\":null,\"wuliu\":null},\"salelist\":[{\"time\":null,\"sale\":null}]}]}";
String data = "{\"enter\":{\"name\":\"衍金\",\"id\":\"123as\"},\"shoplist\":[{\"basic\":{\"name\":\"淘宝\",\"id\":\"13ff4\",\"addr\":\"杭州\"},\"service\":{\"aftersale\":4.5,\"wuliu\":5.5},\"salelist\":[{\"time\":\"201801\",\"sale\":1456.67,\"per\":0.14},{\"time\":\"201802\",\"sale\":2456.67,\"per\":0.24},{\"time\":\"201803\",\"sale\":3456.67,\"per\":0.34},{\"time\":\"201804\",\"sale\":4456.67,\"per\":0.44}]},{\"basic\":{\"name\":\"天猫\",\"id\":\"23ff4\",\"addr\":\"上海\"},\"service\":{\"aftersale\":2.5,\"wuliu\":2.5},\"salelist\":[{\"time\":\"201801\",\"sale\":1156.67,\"per\":0.11},{\"time\":\"201802\",\"sale\":2156.67,\"per\":0.21},{\"time\":\"201803\",\"sale\":3156.67,\"per\":0.31},{\"time\":\"201804\",\"sale\":4156.67,\"per\":0.41}]},{\"basic\":{\"name\":\"京东\",\"id\":\"33ff4\",\"addr\":\"深圳\"},\"service\":{\"aftersale\":3.5,\"wuliu\":3.5},\"salelist\":[{\"time\":\"201801\",\"sale\":1356.67,\"per\":0.13},{\"time\":\"201802\",\"sale\":2356.67,\"per\":0.23},{\"time\":\"201803\",\"sale\":3356.67,\"per\":0.33},{\"time\":\"201804\",\"sale\":4356.67,\"per\":0.43}]}]}";
Object mask = JSONObject.parseObject(a);
Object obj = JSONObject.parseObject(data);
Object result = solution.maskJson(obj, mask);
System.out.println();
}
private Object maskJson(Object obj, Object mask){
if(!(mask instanceof JSONObject) && !(mask instanceof JSONArray)){
return obj;
}else if(mask instanceof JSONArray){
JSONArray jsonArray = new JSONArray();
Object temp = ((JSONArray)mask).iterator().next();
Iterator iterator2 = ((JSONArray)obj).iterator();
while(iterator2.hasNext()){
jsonArray.add(maskJson(iterator2.next(),temp));
}
}else{
Iterator<String> iterator = ((JSONObject)mask).keySet().iterator();
while (iterator.hasNext()) {
String key = iterator.next();
Object newMask = ((JSONObject)mask).get(key);
Object newObject = ((JSONObject)obj).get(key);
((JSONObject)mask).put(key, maskJson(newObject, newMask));
}
}
return mask;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.