How to iterate a JsonObject (gson)

前端 未结 5 1097
有刺的猬
有刺的猬 2021-02-19 08:50

I have a JsonObject e.g

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

Every JsonObject contains

相关标签:
5条回答
  • 2021-02-19 09:23

    Your Map values are JsonElements. Whenever you print a JsonElement (e.g. using a debugger) its toString() method will be called - and since a JsonElement has many implementing classes the default toString implementation wraps the value in quotes to ensure correct JSON. To get the value as a normal, unwrapped String, simply call getAsString():

    JsonElement elem;
    // ...
    String value = elem.getAsString();
    

    With your example:

    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()).getAsString());
    }
    

    Note there are many other methods you can call on a JsonElement to produce other types.

    0 讨论(0)
  • 2021-02-19 09:33

    How are you creating 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 is working fine. Now I am interested in knowing how you created that JSON of yours?

    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);
        }
    
    0 讨论(0)
  • 2021-02-19 09:36

    replace "" with blank.

       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("\"",""));
        }
       }
    
    0 讨论(0)
  • 2021-02-19 09:37

    Just make following changes...

    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()).getAsString());
    }
    

    "getAsString" will do the magic for u

    0 讨论(0)
  • 2021-02-19 09:43

    I am not quite sure why you want to do manual manipulation in the first place. GSON decode will simply leave those absent key/value pairs as default value (zero,null). And then you can process as you want.

    0 讨论(0)
提交回复
热议问题