英文:
create json based on comma separated string in java?
问题
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class DataProcessing {
public static void main(String[] args) {
String inputFilePath = "path/to/your/input/file.txt";
String outputFilePath = "path/to/your/output/output.json";
Map<String, Map<String, String>> dataMap = new HashMap<>();
try (BufferedReader br = new BufferedReader(new FileReader(inputFilePath))) {
String line;
while ((line = br.readLine()) != null) {
String[] parts = line.split(",");
if (parts.length == 3) {
String year = parts[0];
String category = parts[1];
String value = parts[2];
dataMap.putIfAbsent(year, new HashMap<>());
Map<String, String> innerMap = dataMap.get(year);
innerMap.putIfAbsent(category, "");
innerMap.put(category, innerMap.get(category) + ", " + value);
}
}
} catch (IOException e) {
e.printStackTrace();
}
try (FileWriter fw = new FileWriter(outputFilePath)) {
fw.write("{\n");
for (String year : dataMap.keySet()) {
fw.write("\t\"" + year + "\" : {\n");
Map<String, String> innerMap = dataMap.get(year);
for (String category : innerMap.keySet()) {
String values = innerMap.get(category).substring(2); // Remove the initial comma and space
fw.write("\t\t\"" + category + "\" : [" + values + "],\n");
}
fw.write("\t},\n");
}
fw.write("}");
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Data processing and conversion completed.");
}
}
Note: Replace "path/to/your/input/file.txt"
and "path/to/your/output/output.json"
with the actual paths for your input data file and desired output JSON file, respectively. This Java code reads the input data from the file, processes it, and then writes the formatted JSON output. Make sure you have the required permission to read and write files in the specified locations.
英文:
input data in a file given below
1985,Adv,Blue
1985,Adv,gill
1985,Adv,mon
1985,Cal,20
1985,Cal,25
1985,Cape,Din
1966,Ray,One
1966,Ray,bel
1966,Ray,Reb
1966,Sum,37
1966,Tar,Black
1966,Tar,Watch
1967,Yachts,Nut
1967,Yachts,Shark
1967,Cal,20
1967,Cal,25
1967,Cal,28
Expected output as a json file with formatted data like
{
"1985" : {
"Adv" : ["Blue", "gill", "mon"],
"Cal" : ["20", "25"],
"Cape" : ["Din"]
},
"1966" : {
"Ray" : ["One", "bel", "Reb"],
"Sum" : ["37"],
"Tar" : ["Black", "Watch"]
},
"1967" : {
"Yachts" : ["Nut", "Shark"],
"Cal" : ["20", "25", "28"]
}
}
I have more than 1000 lines of data. Need to use some loop. How to do this in java
答案1
得分: 2
你需要导入外部库 org.json.JSONObject
File myObj = new File("test.txt");
Scanner myReader = new Scanner(myObj);
List<String> stringList = new ArrayList<>();
while (myReader.hasNextLine()) {
String data = myReader.nextLine();
stringList.add(data);
}
Map<String, Map<String,List<String>>> mapStringToObject = new HashMap<>();
for(String string : stringList){
String[] data = string.split(",");
if(!mapStringToObject.containsKey(data[0])){
Map<String,List<String>> mapOfLists = new HashMap<>();
List<String> list = new ArrayList<>();
list.add(data[2]);
mapOfLists.put(data[1],list);
mapStringToObject.put(data[0],mapOfLists);
}else{
if(!mapStringToObject.get(data[0]).containsKey(data[1])){
List<String> list = new ArrayList<>();
list.add(data[2]);
mapStringToObject.get(data[0]).put(data[1],list);
}else
mapStringToObject.get(data[0]).get(data[1]).add(data[2]);
}
}
JSONObject json = new JSONObject(mapStringToObject);
System.out.println(json);
myReader.close();
test.txt
1985,Adv,Blue
1985,Adv,gill
1985,Adv,mon
1985,Cal,20
1985,Cal,25
1985,Cape,Din
1966,Ray,One
1966,Ray,bel
1966,Ray,Reb
1966,Sum,37
1966,Tar,Black
1966,Tar,Watch
1967,Yachts,Nut
1967,Yachts,Shark
1967,Cal,20
1967,Cal,25
1967,Cal,28
英文:
You need to import an external library org.json.JSONObject
File myObj = new File("test.txt");
Scanner myReader = new Scanner(myObj);
List<String> stringList = new ArrayList<>();
while (myReader.hasNextLine()) {
String data = myReader.nextLine();
stringList.add(data);
}
Map<String, Map<String,List<String>>> mapStringToObject = new HashMap<>();
for(String string : stringList){
String[] data = string.split(",");
if(!mapStringToObject.containsKey(data[0])){
Map<String,List<String>> mapOfLists = new HashMap<>();
List<String> list = new ArrayList<>();
list.add(data[2]);
mapOfLists.put(data[1],list);
mapStringToObject.put(data[0],mapOfLists);
}else{
if(!mapStringToObject.get(data[0]).containsKey(data[1])){
List<String> list = new ArrayList<>();
list.add(data[2]);
mapStringToObject.get(data[0]).put(data[1],list);
}else
mapStringToObject.get(data[0]).get(data[1]).add(data[2]);
}
}
JSONObject json = new JSONObject(mapStringToObject);
System.out.println(json);
myReader.close();
test.txt
1985,Adv,Blue
1985,Adv,gill
1985,Adv,mon
1985,Cal,20
1985,Cal,25
1985,Cape,Din
1966,Ray,One
1966,Ray,bel
1966,Ray,Reb
1966,Sum,37
1966,Tar,Black
1966,Tar,Watch
1967,Yachts,Nut
1967,Yachts,Shark
1967,Cal,20
1967,Cal,25
1967,Cal,28
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论