translate.java 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package com.java110.code;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.alibaba.fastjson.parser.Feature;
  4. import com.java110.utils.util.StringUtil;
  5. import java.io.BufferedReader;
  6. import java.io.File;
  7. import java.io.FileReader;
  8. import java.util.Set;
  9. /**
  10. * @ClassName translate
  11. * @Description TODO
  12. * @Author wuxw
  13. * @Date 2022/4/21 10:37
  14. * @Version 1.0
  15. * add by wuxw 2022/4/21
  16. **/
  17. public class translate {
  18. public static void main(String[] args) throws Exception {
  19. String enJsonStr = getJsonStr("C:\\Users\\Administrator\\Documents\\project\\hc\\MicroCommunity\\java110-generator\\src\\main\\java\\com\\java110\\code\\enJson.json");
  20. String cnJsonStr = getJsonStr("C:\\Users\\Administrator\\Documents\\project\\hc\\MicroCommunity\\java110-generator\\src\\main\\java\\com\\java110\\code\\cnJson.json");
  21. JSONObject enJson = JSONObject.parseObject(enJsonStr, Feature.OrderedField);
  22. JSONObject cnJson = JSONObject.parseObject(cnJsonStr, Feature.OrderedField);
  23. for (String key : cnJson.keySet()) {
  24. Object keyValue = cnJson.get(key);
  25. if (keyValue instanceof JSONObject) {
  26. JSONObject keyObj = cnJson.getJSONObject(key);
  27. int keyIndex = 0;
  28. for(String subKeyObj :keyObj.keySet()){
  29. String value = getObjValue(enJson,key,keyIndex);
  30. keyObj.put(subKeyObj,value);
  31. keyIndex++;
  32. }
  33. }
  34. }
  35. System.out.println(cnJson.toJSONString());
  36. }
  37. public static String getObjValue(JSONObject enJson, String objKey, int keyIndex) {
  38. JSONObject jsonObject = null;
  39. if(StringUtil.isEmpty(objKey)){
  40. jsonObject = enJson;
  41. }else{
  42. jsonObject = enJson.getJSONObject(objKey);
  43. }
  44. if(jsonObject == null){
  45. return "";
  46. }
  47. int index = 0;
  48. for (String key : jsonObject.keySet()) {
  49. if (index == keyIndex) {
  50. return jsonObject.getString(key);
  51. }
  52. index++;
  53. }
  54. return "";
  55. }
  56. public static String getJsonStr(String jsonStr) throws Exception {
  57. File file = new File(jsonStr);
  58. BufferedReader in = new BufferedReader(new FileReader(file));
  59. String str;
  60. String context = "";
  61. while ((str = in.readLine()) != null) {
  62. context += (str + "\n");
  63. //doDealHtmlNode(str,fileName);
  64. }
  65. return context;
  66. }
  67. }