Read JSON string in JAVA

I want to read the string as JSON format (it should not be JSON, but it is similar to JSON format) and present it in hashMap (key: Keyword, value: COUNT)

for example, suppose I have a String.

String s ={"Welcome":1,"Hi":2,"Hello":1,"Jin":1}; 

Then make it a classification (for the key Hashmap β†’ word, value β†’ number). The end result will be approximately as shown below.

HashMap<String,String> result;

result.get("Jin"); // output : 1
result.get("Hi"); // output : 2

but my codes, this is not going the right way.

JSONParser parser = new JSONParser();
        Object obj = parser.parse(s);
        JSONArray array = new JSONArray();
        array.add(obj);

        System.out.println(array.get(0)); //output: {"Welcome":1,"Hi":2,"Hello":1,"Jin":1}

is it possible with json? or should I smash them one by one? (e.g. split them into "," and ":" ... so on)

Please give me your good advice.

+4
source share
5 answers

Try a snapshot of the code below.

public static void main(final String[] args) throws ParseException {
        String s = "{\"Welcome\":1,\"Hi\":2,\"Hello\":1,\"Jin\":1}";
        JSONParser parser = new JSONParser();
        HashMap<String, Long> obj = (HashMap<String, Long>) parser.parse(s);
        for(String key : obj.keySet()) {
            System.out.println("Key:" + key + " value:" + obj.get(key));
        }
    }
+1
source

org.json .

.

String s = "{\"Welcome\":1,\"Hi\":2,\"Hello\":1,\"Jin\":1}";
JSONObject result = new JSONObject(s);

System.out.println(result.get("Jin")); // output : 1
System.out.println(result.get("Hi")); // output : 2
+1

a json object a array...

:

JSONObject jsonObj = new JSONObject(jsonString.toString());
0

The easiest way to do this is with JACKSON parsers.

import java.util.HashMap;
import java.util.Map;

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

String s = "{\"Welcome\":1,\"Hi\":2,\"Hello\":1,\"Jin\":1}";
ObjectMapper mapper = new ObjectMapper();


Map<String, String> map = mapper.readValue(s, new TypeReference<HashMap<String, String>>() {
});

map.forEach((k, v) -> System.out.println("Key is " + k + " value is " + v));

Fingerprints:

Key is Hi value is 2
Key is Hello value is 1
Key is Welcome value is 1
Key is Jin value is 1
0
source

Use Google JSON ie gson library (2.6.2) and your problem will be solved.

Pay attention to the following code

import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

public class StackOverFlowQuestionset {

    public static void main(String[] args) {
        String s ="{\"Welcome\":1,\"Hi\":2,\"Hello\":1,\"Jin\":1}"; 

        HashMap<String,String> result=new HashMap<String,String>();

        Gson gson = new Gson();

        JsonElement jsonElement = gson.fromJson(s, JsonElement.class);

        JsonObject jsonObject = jsonElement.getAsJsonObject();

        Set<Entry<String, JsonElement>> jsonEntrySet = jsonObject.entrySet();

        for(Entry<String, JsonElement> entry:jsonEntrySet){
            result.put(entry.getKey(), entry.getValue().toString());
        }

        System.out.println(result.get("Jin"));
        System.out.println(result.get("Welcome"));
        System.out.println(result.get("Hi"));
    }
}
0
source

Source: https://habr.com/ru/post/1653769/


All Articles