如何从 java 中的逗号分隔字符串值创建 json?

how to create a json from comma separated string values in java?

在下面给出的文件中输入数据。有 7 列和超过 300k 行。我看到了这个答案 () 但我不能为 7 列做同样的事情。

1985,Adv,Blue,RB,O,120,F,18
1985,Adv,Blue Temp,RB,O,120,F,18
1966,Ray,One,CC,O,300,F,23
1966,Ray,One,CC,O,300,F,27
1966,Sum,37,SF,O,450,M,28

预期输出为 json 文件,格式数据如

    {
    "1985": {
        "Adv": {
            "Blue": {
                "RB": {
                    "O": {
                        "120": {
                            "F": ["18"]
                        }
                    }
                }
            },
            "Blue Temp": {
                "RB": {
                    "O": {
                        "120": {
                            "F": ["18"]
                        }
                    }
                }
            }
        }
    },
    "1966": {
        "Ray": {
            "One": {
                "CC": {
                    "O": {
                        "300": {
                            "F": ["23", "27"]
                        }
                    }
                }
            }
        },
        "Sum": {
            "37": {
                "SF": {
                    "O": {
                        "450": {
                            "M": ["28"]
                        }
                    }
                }
            }
        }
    }
}

下面是我试过的给定代码。如果您需要,请进行一些更改

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

import org.json.JSONObject;

public class Whosebug2 {
    public static void main(String[] args) throws FileNotFoundException { 

        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, Map<String, Map<String, Map<String, Map<String, Map<String,List<String>>>>>>>> mapStringToObject = new HashMap<>();

        Map<String, Map<String, Map<String, Map<String,List<String>>>>> mapStringToObjectElse3 = new HashMap<>();
        Map<String, Map<String, Map<String,List<String>>>> mapStringToObjectElse4 = new HashMap<>();

        Map<String, Map<String,List<String>>> mapStringToObjectElse5 = new HashMap<>();
        Map<String,List<String>>  mapOfListsElse7 = new HashMap<>();
        List<String> listElse7 = new ArrayList<>();

        List<String> list1 = new ArrayList<>();
        List<String> list2 = new ArrayList<>();
        List<String> list3 = new ArrayList<>();
        List<String> list4 = new ArrayList<>();
        List<String> list5 = new ArrayList<>();



        for(String string : stringList){
            String[] data = string.split(",");
            if(!mapStringToObject.containsKey(data[0])){
                Map<String,List<String>>  mapOfLists7 = new HashMap<>();
                List<String> list7 = new ArrayList<>();
                list7.add(data[7]);
                mapOfLists7.put(data[6],list7);
                Map<String, Map<String,List<String>>> mapStringToObject7 = new HashMap<>();
                mapStringToObject7.put(data[5], mapOfLists7);
                Map<String, Map<String, Map<String,List<String>>>> mapStringToObject4 = new HashMap<>();
                mapStringToObject4.put(data[4], mapStringToObject7);
                Map<String, Map<String, Map<String, Map<String,List<String>>>>> mapStringToObject3 = new HashMap<>();
                mapStringToObject3.put(data[3], mapStringToObject4);
                Map<String, Map<String, Map<String, Map<String, Map<String,List<String>>>>>> mapStringToObject2 = new HashMap<>();
                mapStringToObject2.put(data[2], mapStringToObject3);
                Map<String, Map<String, Map<String, Map<String, Map<String, Map<String,List<String>>>>>>> mapStringToObject1 = new HashMap<>();
                mapStringToObject1.put(data[1], mapStringToObject2);

                mapStringToObject.put(data[0],mapStringToObject1);
            }else{
                if(!mapStringToObject.get(data[0]).containsKey(data[1])){

                    Map<String, Map<String, Map<String, Map<String, Map<String,List<String>>>>>> mapStringToObjectElseElse2 = new HashMap<>();
                    mapStringToObjectElseElse2.put(data[2], mapStringToObjectElse3);
                    mapStringToObject.get(data[0]).put(data[1],mapStringToObjectElseElse2);

                    if(!mapStringToObject.get(data[0]).get(data[1]).containsKey(data[2])){

                        Map<String, Map<String, Map<String, Map<String,List<String>>>>> mapStringToObjectElseElse3 = new HashMap<>();
                        mapStringToObjectElseElse3.put(data[3], mapStringToObjectElse4);
                        mapStringToObject.get(data[0]).get(data[1]).put(data[2],mapStringToObjectElseElse3);

                        if(!mapStringToObject.get(data[0]).get(data[1]).get(data[2]).containsKey(data[3])){

                            Map<String, Map<String, Map<String,List<String>>>> mapStringToObjectElseElse4 = new HashMap<>();
                            mapStringToObjectElseElse4.put(data[4], mapStringToObjectElse5);
                            mapStringToObject.get(data[0]).get(data[1]).get(data[2]).put(data[3],mapStringToObjectElseElse4);

                            if(!mapStringToObject.get(data[0]).get(data[1]).get(data[2]).get(data[3]).containsKey(data[4])){

                                Map<String, Map<String,List<String>>> mapStringToObjectElseElse5 = new HashMap<>();
                                mapStringToObjectElseElse5.put(data[5], mapOfListsElse7);
                                mapStringToObject.get(data[0]).get(data[1]).get(data[2]).get(data[3]).put(data[4],mapStringToObjectElseElse5);

                                if(!mapStringToObject.get(data[0]).get(data[1]).get(data[2]).get(data[3]).get(data[4]).containsKey(data[5])){

                                    Map<String,List<String>>  mapOfListsElseElse7 = new HashMap<>();
                                    mapOfListsElseElse7.put(data[6], listElse7);
                                    mapStringToObject.get(data[0]).get(data[1]).get(data[2]).get(data[3]).get(data[4]).put(data[5],mapOfListsElseElse7);

                                    if(!mapStringToObject.get(data[0]).get(data[1]).get(data[2]).get(data[3]).get(data[4]).get(data[5]).containsKey(data[6])){
                                        List<String> list = new ArrayList<>();
                                        list.add(data[7]);
                                        mapStringToObject.get(data[0]).get(data[1]).get(data[2]).get(data[3]).get(data[4]).get(data[5]).put(data[6],list);
                                    }else {
                                        mapStringToObject.get(data[0]).get(data[1]).get(data[2]).get(data[3]).get(data[4]).get(data[5]).get(data[6]).add(data[7]);
                                    }

                                }else {
                                    list1.add(data[7]);
                                    mapStringToObject.get(data[0]).get(data[1]).get(data[2]).get(data[3]).get(data[4]).get(data[5]).put(data[6], list1);
                                }

                            }else {
                                list2.add(data[7]);
                                mapOfListsElse7.put(data[6], list2);
                                mapStringToObject.get(data[0]).get(data[1]).get(data[2]).get(data[3]).get(data[4]).put(data[5], mapOfListsElse7);
                            }

                        }else {
                            list3.add(data[7]);
                            mapOfListsElse7.put(data[6], list3);
                            mapStringToObjectElse5.put(data[5], mapOfListsElse7);
                            mapStringToObject.get(data[0]).get(data[1]).get(data[2]).get(data[3]).put(data[4], mapStringToObjectElse5);
                        }

                    }else {
                        list4.add(data[7]);
                        mapOfListsElse7.put(data[6], list4);
                        mapStringToObjectElse5.put(data[5], mapOfListsElse7);
                        mapStringToObjectElse4.put(data[4], mapStringToObjectElse5);
                        mapStringToObject.get(data[0]).get(data[1]).get(data[2]).put(data[3], mapStringToObjectElse4);
                    }

                }else {
                    list5.add(data[7]);
                    mapOfListsElse7.put(data[6], list5);
                    mapStringToObjectElse5.put(data[5], mapOfListsElse7);
                    mapStringToObjectElse4.put(data[4], mapStringToObjectElse5);
                    mapStringToObjectElse3.put(data[3], mapStringToObjectElse4);
                    mapStringToObject.get(data[0]).get(data[1]).put(data[2], mapStringToObjectElse3);
                }
            }
        }
        convertAsJson(mapStringToObject);
        myReader.close();
    }

    public static void convertAsJson(Map<String, Map<String, Map<String, Map<String, Map<String, Map<String, Map<String, List<String>>>>>>>> mapStringToObject){
        JSONObject json = new JSONObject(mapStringToObject);
        try (FileWriter file = new FileWriter("test.json")) {
            file.write(json.toString());
            file.flush();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

我有超过 30 万行数据。需要使用一些循环。如何在 java

中执行此操作

终于在朋友的帮助下解决了这个问题。这是我的代码..

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

import org.json.JSONObject;

public class JsonConversion {

    public static void main(String[] args) throws FileNotFoundException {

        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,Map<String,Map<String,Map<String,Map<String,Map<String,List<String>>>>>>>> mapStringToObject = new HashMap<>();
        for(String string : stringList){
            String[] data = string.split(",");
            if(!mapStringToObject.containsKey(data[0])){
                Map<String,Map<String,Map<String,Map<String,Map<String,Map<String,List<String>>>>>>> mapOfLists = new HashMap<>();
                Map<String,Map<String,Map<String,Map<String,Map<String,List<String>>>>>> mapOfLists1 = new HashMap<>();
                Map<String,Map<String,Map<String,Map<String,List<String>>>>> mapOfLists2 = new HashMap<>();
                Map<String,Map<String,Map<String,List<String>>>> mapOfLists3 = new HashMap<>();
                Map<String,Map<String,List<String>>>  mapOfLists4 = new HashMap<>();
                Map<String,List<String>>  mapOfLists5 = new HashMap<>();
                List<String> list = new ArrayList<>();
                list.add(data[7]);
                mapOfLists5.put(data[6],list);
                mapOfLists4.put(data[5], mapOfLists5);
                mapOfLists3.put(data[4],mapOfLists4);
                mapOfLists2.put(data[3], mapOfLists3);
                mapOfLists1.put(data[2], mapOfLists2);
                mapOfLists.put(data[1], mapOfLists1);
                mapStringToObject.put(data[0],mapOfLists);
            }else {
                if(!mapStringToObject.get(data[0]).containsKey(data[1])) {
                    Map<String,Map<String,Map<String,Map<String,Map<String,List<String>>>>>> mapOfLists = new HashMap<>();
                    Map<String,Map<String,Map<String,Map<String,List<String>>>>> mapOfLists1 = new HashMap<>();
                    Map<String,Map<String,Map<String,List<String>>>> mapOfLists2 = new HashMap<>();
                    Map<String,Map<String,List<String>>>  mapOfLists3 = new HashMap<>();
                    Map<String,List<String>>  mapOfLists4 = new HashMap<>();
                    List<String> list = new ArrayList<>();
                    list.add(data[7]);
                    mapOfLists4.put(data[6],list);
                    mapOfLists3.put(data[5],mapOfLists4);
                    mapOfLists2.put(data[4], mapOfLists3);
                    mapOfLists1.put(data[3], mapOfLists2);
                    mapOfLists.put(data[2], mapOfLists1);
                    mapStringToObject.get(data[0]).put(data[1],mapOfLists);
                }
                else {
                    if(!mapStringToObject.get(data[0]).get(data[1]).containsKey(data[2])) {
                        List<String> list = new ArrayList<>();
                        list.add(data[7]);
                        Map<String,List<String>>  mapOfLists3 = new HashMap<>();
                        mapOfLists3.put(data[6], list);
                        Map<String,Map<String,List<String>>> mapOfLists2 = new HashMap<>();
                        mapOfLists2.put(data[5], mapOfLists3);
                        Map<String,Map<String,Map<String,List<String>>>> mapOfLists1 = new HashMap<>();
                        mapOfLists1.put(data[4], mapOfLists2);
                        Map<String,Map<String,Map<String,Map<String,List<String>>>>> mapOfLists = new HashMap<>();
                        mapOfLists.put(data[3], mapOfLists1);
                        mapStringToObject.get(data[0]).get(data[1]).put(data[2], mapOfLists);
                    }
                    else {

                        if(!mapStringToObject.get(data[0]).get(data[1]).get(data[2]).containsKey(data[3])) {
                            List<String> list = new ArrayList<>();
                            list.add(data[7]);
                            Map<String,List<String>> mapOfLists2 = new HashMap<>();
                            mapOfLists2.put(data[6], list);
                            Map<String,Map<String,List<String>>> mapOfLists1 = new HashMap<>();
                            mapOfLists1.put(data[5], mapOfLists2);
                            Map<String,Map<String,Map<String,List<String>>>> mapOfLists = new HashMap<>();
                            mapOfLists.put(data[4], mapOfLists1);
                            mapStringToObject.get(data[0]).get(data[1]).get(data[2]).put(data[3],mapOfLists);
                        }
                        else {

                            if(!mapStringToObject.get(data[0]).get(data[1]).get(data[2]).get(data[3]).containsKey(data[4])) {
                                List<String> list = new ArrayList<>();
                                list.add(data[7]);
                                Map<String,List<String>> mapOfLists1 = new HashMap<>();
                                mapOfLists1.put(data[6], list);
                                Map<String,Map<String,List<String>>> mapOfLists = new HashMap<>();
                                mapOfLists.put(data[5], mapOfLists1);
                                mapStringToObject.get(data[0]).get(data[1]).get(data[2]).get(data[3]).put(data[4],mapOfLists);
                            }
                            else {

                                if(!mapStringToObject.get(data[0]).get(data[1]).get(data[2]).get(data[3]).get(data[4]).containsKey(data[5])) {
                                    List<String> list = new ArrayList<>();
                                    list.add(data[7]);
                                    Map<String,List<String>> mapOfLists = new HashMap<>();
                                    mapOfLists.put(data[6], list);
                                    mapStringToObject.get(data[0]).get(data[1]).get(data[2]).get(data[3]).get(data[4]).put(data[5],mapOfLists);
                                }
                                else {

                                    if(!mapStringToObject.get(data[0]).get(data[1]).get(data[2]).get(data[3]).get(data[4]).get(data[5]).containsKey(data[6])) {
                                        List<String> list = new ArrayList<>();
                                        list.add(data[7]);
                                        mapStringToObject.get(data[0]).get(data[1]).get(data[2]).get(data[3]).get(data[4]).get(data[5]).put(data[6],list);
                                    }
                                    else {
                                        mapStringToObject.get(data[0]).get(data[1]).get(data[2]).get(data[3]).get(data[4]).get(data[5]).get(data[6]).add(data[7]);
                                    }

                                }
                            }

                        }

                    }
                }
            }
        }
        JSONObject json = new JSONObject(mapStringToObject);
        System.out.println(json);
        myReader.close();
    }
}