简体   繁体   English

将部分JSON格式的列表数据转换为映射

[英]Convert a List Partially JSON Formatted Data into a Map

I have a following Object Data as below 我有以下对象数据如下

[{
name = john , id=1,email id  : [abc@gmail.com,def@gmail.com,ghi@gmail.com]
},{
name = joy, id=2,email id  : [jkl@gmail.com,mno@gmail.com,pqr@gmail.com]
}]

I want the following to be converted to Map as 我希望将以下内容转换为Map

{john=[abc@gmail.com,def@gmail.com,ghi@gmail.com]},{joy= [jkl@gmail.com,mno@gmail.com,pqr@gmail.com]}

in Java how will I be able to achieve this. 在Java中,我将如何实现这一目标。

Kindly help me with this 请帮助我

no easy way of doing this, so much hard manual work with so many possibilities for errors, you can use this. 做到这一点绝非易事,繁琐的手工工作以及很多错误的可能性,可以使用。 as long as your data is EXACTLY as shown then this should work. 只要您的数据与显示的完全一样,那么它应该起作用。

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;




public class Person {




    int id;
    String name;
    List<String> emails;




    public static Person parse(String s) throws Exception {

        Person person = new Person();
        person.name = s.substring(0, s.indexOf(",")).replaceAll(".+= ?", "");
        s = s.substring(s.indexOf(",") + 1, s.length());
        person.id = Integer.parseInt(s.substring(0, s.indexOf(",")).replaceAll(".+= ?", "").trim());
        s = s.substring(s.indexOf(",") + 1, s.length());
        s = s.replaceAll(".+: ?", "").replaceAll("[ \\[\\]]", "");
        person.emails = Arrays.asList(s.split(","));

        return person;

    }




    public static List<Person> parseMany(String s) throws Exception {

        List<Person> people = new ArrayList<Person>();

        while (true){
            int openBracket = s.indexOf("{");
            int closeBracket = s.indexOf("}");

            if(closeBracket == -1){
                break;
            }
            people.add(parse(s.substring(openBracket, closeBracket)));
            s = s.substring(closeBracket + 1, s.length());

        }

        return people;

    }




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

        String s = "[{name = john , id=1,email id  : [abc@gmail.com,def@gmail.com,ghi@gmail.com]},{name = joy, id=2,email id  : [jkl@gmail.com,mno@gmail.com,pqr@gmail.com]}]";

        List<Person> people = parseMany(s);

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

        for(Person person : people){
            map.put(person.name, person.emails);
        }

        System.out.println(map);

    }

}

OUTPUT: 输出:

{joy=[jkl@gmail.com, mno@gmail.com, pqr@gmail.com], john =[abc@gmail.com, def@gmail.com, ghi@gmail.com]} {joy = [jkl@gmail.com,mno@gmail.com,pqr@gmail.com],john = [abc@gmail.com,def@gmail.com,ghi@gmail.com]}

NOTE: 注意:

I didn't bother to comment this code, because problem doesn't follow any standard procedures. 我没有打扰评论此代码,因为问题不遵循任何标准程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM