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

Posted

技术标签:

【中文标题】如何在java中从逗号分隔的字符串值创建一个json?【英文标题】:how to create a json from comma separated string values in java? 【发布时间】:2020-05-15 14:18:45 【问题描述】:

在下面给出的文件中输入数据。有 7 列和超过 300k 行。我看到了这个答案 (https://***.com/a/61106999/10186590),但我不能为 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 ***2 
    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中做到这一点

【问题讨论】:

@user:6909408 你有答案吗 可以一行吗?还是必须用空格和换行符创建这种精确的格式? 可以是一行,以后可以格式化。 您能否编辑您的问题以向我们展示您已经尝试过的内容?您使用的是哪个 JSON 库?具体来说,您在哪里遇到问题? (此外,在使用“十万”这个词时可能要小心——一些评论者可能不熟悉它。) @andrewjames 是的,我也添加了我的代码。 【参考方案1】:

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

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();
    

【讨论】:

以上是关于如何在java中从逗号分隔的字符串值创建一个json?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 JavaScript 中使用逗号分隔符从字符串中生成数字(“23,21”---> 23,21)

PHP:使用逗号分隔值创建字符串,但字符串末尾没有逗号

如何将字符串(包含逗号分隔值)转换为整数

如何使用 Angular js 或 java 脚本将逗号分隔的字符串分解为单个索引数组

通过连接来自不同列的逗号分隔值来创建字符串

优化逗号分隔值正则表达式