1

我有这个看起来像这样的 csv

givenName,familyNames,formattedName,middleName,honorificPrefix,honorificSuffix,Department.displayName,UserType.shortName,Address.address1,Address.address2,Email.type,Email.value
Jim,jackson,jimJackson,none,mr,none,Product,jack,Houston,USA,home,jim@gmail.com
null,jackson,null,null,null,null,null,null,Dallas,USA,work,jackson@workmail.com
null,null,null,null,null,null,null,null,null,null,office,jackie@office.com
john,lasher,johnLasher,none,mr,none,Operations,lash,Austin,USA,home,lasher@gmail.com

现在,我想将所有这些数据绑定到标题行并将其转换为 json,此外,如果它们是多行的多行,则应将其转换为单个 JSON,因此上述数据应生成两个 JSON 文档,例如这个

{
"UserType": [
    {
        "shortName": "jack"
    }
],
"middleName": "none",
"familyNames": "jackson",
"Email": [
    {
        "value": "jim@gmail.com",
        "type": "home"
    },
    {
        "value": "jackson@workmail.com",
        "type": "work"
    },
    {
        "value": "jackie@office.com",
        "type": "office"
    }
],
"Address": [
    {
        "address1": "Houston",
        "address2": "USA"
    },
    {
        "address1": "Dallas",
        "address2": "USA"
    }
],
"honorificPrefix": "mr",
"formattedName": "jimJackson",
"givenName": "Jim",
"Department": [
    {
        "displayName": "Product"
    }
],
"honorificSuffix": "none"
}
{
"UserType": [
    {
        "shortName": "lash"
    }
],
"middleName": "none",
"familyNames": "lasher",
"Email": [
    {
        "value": "lasher@gmail.com",
        "type": "home"
    }
],
"Address": [
    {
        "address1": "Austin",
        "address2": "USA"
    }
],
"honorificPrefix": "mr",
"formattedName": "johnLasher",
"givenName": "john",
"Department": [
    {
        "displayName": "Operations"
    }
],
"honorificSuffix": "none"
}

我正在使用 jcsv 来读取 csv,这就是我的代码的样子,我得到了可以将数据绑定到 JSON 中的部分,但我需要一些帮助才能在单个 json 中获取多值。

CSVReader<String[]> csvPersonReader = CSVReaderBuilder.newDefaultReader(new       FileReader("addresses.csv"));
    List<String[]> persons = csvPersonReader.readAll();
    JSONArray finalArray = new JSONArray();
    String temp1 = null;
    String temp2 = null;
    String duplicateField = "familyNames";
    String tempStored = null;
    ArrayList<String> multiValueAttributes = new ArrayList<String>();
    int c = 0;
    String delimiter = "\\.";

    for (int j = 1; j < persons.size(); j++) {
        for (int i = 0; i < persons.get(j).length; i++) {
            StringTokenizer headerValues = new StringTokenizer(persons.get(0)[i]);
            StringTokenizer bindingValues = new StringTokenizer(persons.get(j)[i]);
            JSONObject hasher = new JSONObject();
            ArrayList<String> EntitiesWithMultipleAttributes = new ArrayList<String>();
            JSONObject multiValueJSON = new JSONObject();
            int counter = 0;
            int finalValue = 0;
            int abc = 0;
            while (headerValues.hasMoreTokens() && bindingValues.hasMoreTokens()) {
                temp1 = headerValues.nextToken(",");
                temp2 = bindingValues.nextToken(",");
                if (temp1.equals(duplicateField) && multiValueAttributes.contains(temp2)) {
                    counter++;
                } else if (temp1.equals(duplicateField) && !multiValueAttributes.contains(temp2)) {
                    multiValueAttributes.add(c, temp2);
                    c++;
                    tempStored = temp2;
                }
                if (temp1.contains(".")) {
                    JSONArray complexEntity = new JSONArray();
                    String[] entities = temp1.split(delimiter);

                    } else if (EntitiesWithMultipleAttributes.contains(entities[0])) {
                        hasher.getJSONArray(entities[0]).getJSONObject(0).put(entities[1], temp2);
                    } else {
                        EntitiesWithMultipleAttributes.add(entities[0]);
                        JSONObject multiValueJSONFirst = new JSONObject();
                        multiValueJSONFirst.put(entities[1], temp2);
                        complexEntity.put(multiValueJSONFirst);
                        hasher.put(entities[0], complexEntity);
                    }

                } else {
                    hasher.put(temp1, temp2);
                }

            }

                finalArray.put(hasher);
        }
    }

我没有任何 POJO,也无法创建一个,因此无法使用 Beans。

4

0 回答 0