我有这个看起来像这样的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是不可能的。