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