How to drop keys from string json in java?

后端 未结 2 1731
借酒劲吻你
借酒劲吻你 2021-01-26 06:44

I have a json string like this:

{\"a\":\"vala\", \"b\":\"valb\", \"c\":\"valc\"}

I want to convert the above string to a JSONObject so that I

2条回答
  •  礼貌的吻别
    2021-01-26 06:55

    org-json-java can do the things you want

    import org.json.JSONException;
    import org.json.JSONObject;
    
    public class JsonTest {
        public static void main(String[] args) {
            try {
                JSONObject testObject = new JSONObject("{\"a\":\"vala\", \"b\":\"valb\", \"c\":\"valc\"}");
                testObject.remove("b");
                testObject.remove("c");
                System.out.println(testObject.toString());  // This prints out the json string
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
    

    The execution result is

    {"a":"vala"}
    

    Make sure you've downloaded and imported org.json-YYYYMMDD.jar from here before you run the above code.

提交回复
热议问题