Note: The ask is on creating a stream of numbers from a json string, preferably using regex. Please do not consider the question as a duplicate of something else on regex. My main objective is to read a stream of numbers. Using regex for the purpose is optional.
My objective is to create an array of decimal numbers read from a large json string that looks like the example shown below.
Example input:
{
"item1": 102.119,
"item2": "unknown",
"item3": 200.12,
"item4": 1.08,
"item5": 0.04
}
Expected output:
My Data = [102.119, 200.12, 1.08, 0.04]
What I tried:
import java.util.ArrayList;
public class MyData {
public static void main(String[] args) {
String input = ("{\"item1\": 102.119,"
+ "\"item2\": \"unknown\","
+ "\"item3\": 200.12,"
+ "\"item4\": 1.08,"
+ "\"item5\": 0.04}"
);
ArrayList<Double> output = new ArrayList<>();
for(String s1 : input.split(",")) {
for(String s2: s1.split(":")) {
try {
output.add(Double.parseDouble(s2));
} catch(Exception e) {
System.out.println(e.toString() + " : " + s2 + " is not a number!");
}
}
}
System.out.println("\nMy Data = " + output.toString());
}
}
Actual output:
$ javac MyData.java
$ java MyData
java.lang.NumberFormatException: For input string: "{"item1"" : {"item1" is not a number!
java.lang.NumberFormatException: For input string: ""item2"" : "item2" is not a number!
java.lang.NumberFormatException: For input string: ""unknown"" : "unknown" is not a number!
java.lang.NumberFormatException: For input string: ""item3"" : "item3" is not a number!
java.lang.NumberFormatException: For input string: ""item4"" : "item4" is not a number!
java.lang.NumberFormatException: For input string: ""item5"" : "item5" is not a number!
java.lang.NumberFormatException: For input string: "0.04}" : 0.04} is not a number!
My Data = [102.119, 200.12, 1.08]
Problems:
- The final number 0.04 is not read into the data array.
- The two for loops are taking long time to execute on a large input set.
Help required:
Can you please help improve the code to read all the decimal numbers, and in a more efficient way?
Can you let me know if regular expression pattern matching can be used for this scenario?