问题描述:

I have the following string:

"value":"ValueNumber1","label":"Peppe "},{"value":"ValueNumber2","label":"LoL"

I would like to split this string and get only the content of "value", in this case for example I would like to retrieve "ValueNumber1" and "ValueNumber2" (without double quotes).

I have tried something like this but without success:

StringToBeParsed.split("value\":\"+.\""));

Do you have any suggestion ?

Tnx in advance.

网友答案:

You can use this regex:

value":"(.*?)"

Working demo

MATCH 1
1.  [9-21]  `ValueNumber1`
MATCH 2
1.  [51-63] `ValueNumber2`
网友答案:

Using Json

public static void main(String[] args) {

        String s = "{\"values\":[{\"value\":\"ValueNumber1\",\"label\":\"Peppe\"},{\"value\":\"ValueNumber2\",\"label\":\"LoL\"}]}";
        JSONObject jObject;
        try {
            jObject = new JSONObject(s);
            JSONArray the_json_array = jObject.getJSONArray("values");

            int size = the_json_array.length();
            for (int i = 0 ; i < size ; i++) {
                JSONObject another_json_object = the_json_array.getJSONObject(i);
                System.out.println(another_json_object.get("value"));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
相关阅读:
Top