基于逗号分隔的字符串在Java中创建JSON?

huangapple go评论73阅读模式
英文:

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

{
&quot;1985&quot; : {
&quot;Adv&quot; : [&quot;Blue&quot;, &quot;gill&quot;, &quot;mon&quot;],
&quot;Cal&quot; : [&quot;20&quot;, &quot;25&quot;],
&quot;Cape&quot; : [&quot;Din&quot;]
},
&quot;1966&quot; : {
&quot;Ray&quot; : [&quot;One&quot;, &quot;bel&quot;, &quot;Reb&quot;],
&quot;Sum&quot; : [&quot;37&quot;],
&quot;Tar&quot; : [&quot;Black&quot;, &quot;Watch&quot;]
},
&quot;1967&quot; : {
&quot;Yachts&quot; : [&quot;Nut&quot;, &quot;Shark&quot;],
&quot;Cal&quot; : [&quot;20&quot;, &quot;25&quot;, &quot;28&quot;]
}
}

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(&quot;test.txt&quot;);
Scanner myReader = new Scanner(myObj);
List&lt;String&gt; stringList = new ArrayList&lt;&gt;();
while (myReader.hasNextLine()) {
String data = myReader.nextLine();
stringList.add(data);
}
Map&lt;String, Map&lt;String,List&lt;String&gt;&gt;&gt; mapStringToObject = new HashMap&lt;&gt;();
for(String string : stringList){
String[] data = string.split(&quot;,&quot;);
if(!mapStringToObject.containsKey(data[0])){
Map&lt;String,List&lt;String&gt;&gt;  mapOfLists = new HashMap&lt;&gt;();
List&lt;String&gt; list = new ArrayList&lt;&gt;();
list.add(data[2]);
mapOfLists.put(data[1],list);
mapStringToObject.put(data[0],mapOfLists);
}else{
if(!mapStringToObject.get(data[0]).containsKey(data[1])){
List&lt;String&gt; list = new ArrayList&lt;&gt;();
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

huangapple
  • 本文由 发表于 2020年4月7日 01:02:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/61065033.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定