简体   繁体   English

从Java字符串中提取值

[英]Extract values from a Java String

I have Java string('s) in the format below: 我有以下格式的Java字符串:

String s = "[
    "samsung",

    ["samsung galaxy s9 case","samsung galaxy s8 case","samsung galaxy s9 plus case","samsung galaxy s8 charger"],

    [{"nodes":[{"name":"Cell Phones & Accessories","alias":"mobile"}]},{},{},{},{},{},{},{},{},{}],

    [],

    "1XQ3CN8WM8VSE"
]"

What's the best way to process the String so I can get these values (of 2nd item enclosed with []) 什么是处理String的最佳方法,以便我可以获取这些值(用[]括起来的第二项)

"samsung galaxy s9 case","samsung galaxy s8 case","samsung galaxy s9 plus case","samsung galaxy s8 charger"

inside a List<String> ? List<String>

Update 更新

The String is valid JSON and tested with the code 字符串是有效的JSON,并已通过代码测试

public static boolean isJSONValid(String test) {
        try {
            new JSONObject(test);
        } catch (JSONException ex) {
            // edited, to include @Arthur's comment
            // e.g. in case JSONArray is valid as well...
            try {
                new JSONArray(test);
            } catch (JSONException ex1) {
                return false;
            }
        }
        return true;
    }

I also tried to parse it as JSON (as suggested) but I get exception provided. 我也尝试将其解析为JSON(如建议的那样),但提供了异常。

JSONObject obj = new JSONObject(s); JSONObject obj =新的JSONObject;

Exception in thread "main" org.json.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1] . 线程“主” org.json.JSONException中的异常:JSONObject文本必须在1 [字符2行1]处以'{'开头。

My String always starts with [..] 我的弦乐总是以[..]开头

That format is not a valid json that´s why you are getting that error, one simple way to get the String you want is using an split method and then storage it in the your prefer collections. 这种格式不是有效的json,这就是为什么您会收到该错误,获取所需String的一种简单方法是使用split方法,然后将其存储在您喜欢的集合中。

public static void main(String args[]){
    String s = "[samsung,[\"samsung galaxy s9 case\",\"samsung galaxy s8 case\",\"samsung galaxy s9 plus case\",\"samsung galaxy s8 charger\"],[{\"nodes\":[{\"name\":\"Cell Phones & Accessories\",\"alias\":\"mobile\"}]},{},{},{},{},{},{},{},{},{}],[],\"1XQ3CN8WM8VSE\"]";

    String[] splitedFullString = s.split(",\\[");
    String sequence = splitedFullString[1];
    sequence = sequence.replaceAll("]", "");
    sequence = sequence.replaceAll("\"", "");
    String[] splitSequence = sequence.split(",");

    List<String> list = new ArrayList<>(); 
    for(String item : splitSequence){
        list.add(item);
    }
    for(String item : list){
        System.out.println(item);
    }
}

I write the code with the help of the answer of Andreas, 我在Andreas的答案的帮助下编写了代码,

    JSONArray obj = new JSONArray(s);

    String str = obj.get(1).toString();
    String[] arr = str.substring(1, str.length()-1).split(",");

This works fine now. 现在工作正常。 Thank you. 谢谢。

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

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