How to repeat JSONObject (gson)

I have a JsonObject for example

JsonObject jsonObject = {"keyInt":2,"keyString":"val1","id":"0123456"} 

Each JSONObject contains an "id" entry, but the number of other key / value pairs is NOT defined, so I want to create an object with two attributes:

 class myGenericObject { Map<String, Object> attributes; String id; } 

So, I want my attribute map to look like this:

 "keyInt" -> 4711 "keyStr" -> "val1" 

I found this solution

 Map<String, Object> attributes = new HashMap<String, Object>(); Set<Entry<String, JsonElement>> entrySet = jsonObject.entrySet(); for(Map.Entry<String,JsonElement> entry : entrySet){ attributes.put(entry.getKey(), jsonObject.get(entry.getKey())); } 

but the values ​​are enclosed in ""

 "keyInt" -> "4711" "keyStr" -> ""val1"" 

How to get simple values ​​(4711 and "val1")?

Input data:

 { "id": 0815, "a": "a string", "b": 123.4, "c": { "a": 1, "b": true, "c": ["a", "b", "c"] } } 

or

 { "id": 4711, "x": false, "y": "y?", } 
+6
source share
3 answers

replace "" with empty.

  Map<String, Object> attributes = new HashMap<String, Object>(); Set<Entry<String, JsonElement>> entrySet = jsonObject.entrySet(); for(Map.Entry<String,JsonElement> entry : entrySet){ if (! nonProperties.contains(entry.getKey())) { properties.put(entry.getKey(), jsonObject.get(entry.getKey()).replace("\"","")); } } 
+6
source

How do you create your JsonObject? Your code works for me. Consider this

 import com.google.gson.JsonElement; import com.google.gson.JsonObject; ... ... ... try{ JsonObject jsonObject = new JsonObject(); jsonObject.addProperty("keyInt", 2); jsonObject.addProperty("keyString", "val1"); jsonObject.addProperty("id", "0123456"); System.out.println("json >>> "+jsonObject); Map<String, Object> attributes = new HashMap<String, Object>(); Set<Entry<String, JsonElement>> entrySet = jsonObject.entrySet(); for(Map.Entry<String,JsonElement> entry : entrySet){ attributes.put(entry.getKey(), jsonObject.get(entry.getKey())); } for(Map.Entry<String,Object> att : attributes.entrySet()){ System.out.println("key >>> "+att.getKey()); System.out.println("val >>> "+att.getValue()); } } catch (Exception ex){ System.out.println(ex); } 

And it works fine. Now I am interested to know how you create your JSON?

You can also try this (JSONObject)

 import org.json.JSONObject; ... ... ... try{ JSONObject jsonObject = new JSONObject("{\"keyInt\":2,\"keyString\":\"val1\",\"id\":\"0123456\"}"); System.out.println("JSON :: "+jsonObject.toString()); Iterator<String> it = jsonObject.keys(); while( it.hasNext() ){ String key = it.next(); System.out.println("Key:: !!! >>> "+key); Object value = jsonObject.get(key); System.out.println("Value Type "+value.getClass().getName()); } } catch (Exception ex){ System.out.println(ex); } 
+1
source

I'm not quite sure why you want to do manual manipulation in the first place. GSON decoding will simply leave these missing key / value pairs as the default value (zero, zero). And then you can process as you want.

0
source

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


All Articles