Click here to Skip to main content
14,837,944 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a method that returns some kind of string. I want to store the individual words in a HashMap with their number of occurrences?

For eg, I have a string- "{link:, matrics:[{name:apple, value:1},{name:graph, value:2},{name:abc, value:0}]}";

Now my hashmap should look like

apple = 1
graph = 2
abc = 0

How should I proceed?

I know how to use HashMaps. My problem, in this case, is that I don't know how to parse through the given string and store the words with their number of occurrences.

What I have tried:

String[] strs = s.split("matrics");
System.out.println("Substrings length:" + strs.length);
for (int i = 0; i < strs.length; i++) {
Updated 8-Mar-20 1:45am

1 solution

That data is in JSON format so you just need to use the correct class to parse the JSON and find the 'name' entries. See JsonObject (JSON Documentation)[^].

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900