[{
"field1": "",
"field2": "I got you",
"field3": [],
"field4": ""
}]
My objective is to read only the field2 value. How can I do it using regex in java?
While it's certainly possible to treat JSON as any regular String, I'd say it's a better idea to extract the value using JSONPath. For your example it would look like this:
String json = ...;
String value2 = JsonPath.read(json, "$[0].field2");
The dollar sign stands for the root element, [0] is the first element of the array and field2 the name of the field you want to extract.
I think it's duplicate of this question.
You can see this article for example if you realy want to use Regex.
But as Thomas says (in first comment to your post), better solution will be use json parser, examples you can see in this article.
field2can be you'll eventually run into problems and even a very complex expression might not always do what you want. Better use a proper parser and extract the value (e.g. use Jackson'sObjectMapper.readTree(json)to get a generic representation of your JSON).