I have a long String, which is a json files contents. Somewhere in this String, it contains the following: "totalWinAmount":100
To find and return this value, I'm currently doing this:
int totalWinAmountIndex = line.lastIndexOf("totalWinAmount");
String resultTotalWinAmount = line.substring(totalWinAmountIndex + 16, totalWinAmountIndex + 19);
This works, resultTotalWinAmount is correctly set to 100, but I'm curious if this is the best practise for a situation like this, whether this is actually awful coding, or if there's a better way of doing it?
You could also try to extract the value with the use of regex. Something like this should work:
That's a bit messy, but works without additional libraries.
However, I agree with Sun. For proper analysis of json you should always use something like Jackson or GSON.