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

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

create json based on comma separated string in java?

问题

  1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7. public class DataProcessing {
  8. public static void main(String[] args) {
  9. String inputFilePath = "path/to/your/input/file.txt";
  10. String outputFilePath = "path/to/your/output/output.json";
  11. Map<String, Map<String, String>> dataMap = new HashMap<>();
  12. try (BufferedReader br = new BufferedReader(new FileReader(inputFilePath))) {
  13. String line;
  14. while ((line = br.readLine()) != null) {
  15. String[] parts = line.split(",");
  16. if (parts.length == 3) {
  17. String year = parts[0];
  18. String category = parts[1];
  19. String value = parts[2];
  20. dataMap.putIfAbsent(year, new HashMap<>());
  21. Map<String, String> innerMap = dataMap.get(year);
  22. innerMap.putIfAbsent(category, "");
  23. innerMap.put(category, innerMap.get(category) + ", " + value);
  24. }
  25. }
  26. } catch (IOException e) {
  27. e.printStackTrace();
  28. }
  29. try (FileWriter fw = new FileWriter(outputFilePath)) {
  30. fw.write("{\n");
  31. for (String year : dataMap.keySet()) {
  32. fw.write("\t\"" + year + "\" : {\n");
  33. Map<String, String> innerMap = dataMap.get(year);
  34. for (String category : innerMap.keySet()) {
  35. String values = innerMap.get(category).substring(2); // Remove the initial comma and space
  36. fw.write("\t\t\"" + category + "\" : [" + values + "],\n");
  37. }
  38. fw.write("\t},\n");
  39. }
  40. fw.write("}");
  41. } catch (IOException e) {
  42. e.printStackTrace();
  43. }
  44. System.out.println("Data processing and conversion completed.");
  45. }
  46. }

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

  1. 1985,Adv,Blue
  2. 1985,Adv,gill
  3. 1985,Adv,mon
  4. 1985,Cal,20
  5. 1985,Cal,25
  6. 1985,Cape,Din
  7. 1966,Ray,One
  8. 1966,Ray,bel
  9. 1966,Ray,Reb
  10. 1966,Sum,37
  11. 1966,Tar,Black
  12. 1966,Tar,Watch
  13. 1967,Yachts,Nut
  14. 1967,Yachts,Shark
  15. 1967,Cal,20
  16. 1967,Cal,25
  17. 1967,Cal,28

Expected output as a json file with formatted data like

  1. {
  2. &quot;1985&quot; : {
  3. &quot;Adv&quot; : [&quot;Blue&quot;, &quot;gill&quot;, &quot;mon&quot;],
  4. &quot;Cal&quot; : [&quot;20&quot;, &quot;25&quot;],
  5. &quot;Cape&quot; : [&quot;Din&quot;]
  6. },
  7. &quot;1966&quot; : {
  8. &quot;Ray&quot; : [&quot;One&quot;, &quot;bel&quot;, &quot;Reb&quot;],
  9. &quot;Sum&quot; : [&quot;37&quot;],
  10. &quot;Tar&quot; : [&quot;Black&quot;, &quot;Watch&quot;]
  11. },
  12. &quot;1967&quot; : {
  13. &quot;Yachts&quot; : [&quot;Nut&quot;, &quot;Shark&quot;],
  14. &quot;Cal&quot; : [&quot;20&quot;, &quot;25&quot;, &quot;28&quot;]
  15. }
  16. }

I have more than 1000 lines of data. Need to use some loop. How to do this in java

答案1

得分: 2

你需要导入外部库 org.json.JSONObject

  1. File myObj = new File("test.txt");
  2. Scanner myReader = new Scanner(myObj);
  3. List<String> stringList = new ArrayList<>();
  4. while (myReader.hasNextLine()) {
  5. String data = myReader.nextLine();
  6. stringList.add(data);
  7. }
  8. Map<String, Map<String,List<String>>> mapStringToObject = new HashMap<>();
  9. for(String string : stringList){
  10. String[] data = string.split(",");
  11. if(!mapStringToObject.containsKey(data[0])){
  12. Map<String,List<String>> mapOfLists = new HashMap<>();
  13. List<String> list = new ArrayList<>();
  14. list.add(data[2]);
  15. mapOfLists.put(data[1],list);
  16. mapStringToObject.put(data[0],mapOfLists);
  17. }else{
  18. if(!mapStringToObject.get(data[0]).containsKey(data[1])){
  19. List<String> list = new ArrayList<>();
  20. list.add(data[2]);
  21. mapStringToObject.get(data[0]).put(data[1],list);
  22. }else
  23. mapStringToObject.get(data[0]).get(data[1]).add(data[2]);
  24. }
  25. }
  26. JSONObject json = new JSONObject(mapStringToObject);
  27. System.out.println(json);
  28. myReader.close();

test.txt

  1. 1985,Adv,Blue
  2. 1985,Adv,gill
  3. 1985,Adv,mon
  4. 1985,Cal,20
  5. 1985,Cal,25
  6. 1985,Cape,Din
  7. 1966,Ray,One
  8. 1966,Ray,bel
  9. 1966,Ray,Reb
  10. 1966,Sum,37
  11. 1966,Tar,Black
  12. 1966,Tar,Watch
  13. 1967,Yachts,Nut
  14. 1967,Yachts,Shark
  15. 1967,Cal,20
  16. 1967,Cal,25
  17. 1967,Cal,28
英文:

You need to import an external library org.json.JSONObject

  1. File myObj = new File(&quot;test.txt&quot;);
  2. Scanner myReader = new Scanner(myObj);
  3. List&lt;String&gt; stringList = new ArrayList&lt;&gt;();
  4. while (myReader.hasNextLine()) {
  5. String data = myReader.nextLine();
  6. stringList.add(data);
  7. }
  8. Map&lt;String, Map&lt;String,List&lt;String&gt;&gt;&gt; mapStringToObject = new HashMap&lt;&gt;();
  9. for(String string : stringList){
  10. String[] data = string.split(&quot;,&quot;);
  11. if(!mapStringToObject.containsKey(data[0])){
  12. Map&lt;String,List&lt;String&gt;&gt; mapOfLists = new HashMap&lt;&gt;();
  13. List&lt;String&gt; list = new ArrayList&lt;&gt;();
  14. list.add(data[2]);
  15. mapOfLists.put(data[1],list);
  16. mapStringToObject.put(data[0],mapOfLists);
  17. }else{
  18. if(!mapStringToObject.get(data[0]).containsKey(data[1])){
  19. List&lt;String&gt; list = new ArrayList&lt;&gt;();
  20. list.add(data[2]);
  21. mapStringToObject.get(data[0]).put(data[1],list);
  22. }else
  23. mapStringToObject.get(data[0]).get(data[1]).add(data[2]);
  24. }
  25. }
  26. JSONObject json = new JSONObject(mapStringToObject);
  27. System.out.println(json);
  28. myReader.close();

test.txt

  1. 1985,Adv,Blue
  2. 1985,Adv,gill
  3. 1985,Adv,mon
  4. 1985,Cal,20
  5. 1985,Cal,25
  6. 1985,Cape,Din
  7. 1966,Ray,One
  8. 1966,Ray,bel
  9. 1966,Ray,Reb
  10. 1966,Sum,37
  11. 1966,Tar,Black
  12. 1966,Tar,Watch
  13. 1967,Yachts,Nut
  14. 1967,Yachts,Shark
  15. 1967,Cal,20
  16. 1967,Cal,25
  17. 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:

确定