gpt4 book ai didi

java - 将字符串格式转换为列表 - 它不是 json 格式字符串

转载 作者:太空宇宙 更新时间:2023-11-04 10:29:11 25 4
gpt4 key购买 nike

如何将这种格式的字符串转换为列表?

[[["Census_county_divisions","Populated_places_in_the_United_States","Populated_places_by_country","Geography_by_country","Geography_by_place","Geography","Main_topic_classifications"]],[["example","text","thanks"]],[["name","surname","age"]]]

从该字符串中我想要 3 个列表:

列表 1:

"Census_county_divisions","Populated_places_in_the_United_States","Populated_places_by_country","Geography_by_country","Geography_by_place","Geography","Main_topic_classifications"
List 2:"example","text","thanks"
List 3:"name","surname","age"

我尝试过不同的方法来处理这个字符串,使用 split、使用方法 StringUtils.substringBetween、使用 indexOf、使用正则表达式、使用 Json Parser...我总是收到错误,这是一个更简单的方法吗?

评论:我不认为这个字符串是 Json 格式,因为 Json 格式将是“name”:“John”,如果我错了,请告诉我如何将其处理为 Json...

我也尝试过使用 JsonParser 并在线程“main”java.lang.IllegalStateException 中出现异常:不是 JSON 对象:

[[["Census_county_divisions","人口

最佳答案

我写了这段代码:

  1. 删除 [[[,]]] 字符串
  2. 将 ]],[[ 替换为 |字符
  3. 分割字符串

    ///The String to convert
    String arg = "[[[\"Census_county_divisions\",....
    [[\"example\",\"text\",\"thanks\"]],[[\"name\",\"surname\",\"age\"]]]";

    System.out.println(arg);

    ////Replace
    arg = arg.replace("[[[", "");
    arg = arg.replace("]],[[", "|");
    arg = arg.replace("]]]", "");

    System.out.println(arg);

    ////Split
    String[] array=arg.split("\\|");
    List<String> list = Arrays.asList(array);

    ///Verify
    for(String s: list) {
    System.out.println(s);
    }

问候

关于java - 将字符串格式转换为列表 - 它不是 json 格式字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50222284/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com