How to drop keys from string json in java?

后端 未结 2 1730
借酒劲吻你
借酒劲吻你 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.

    0 讨论(0)
  • 2021-01-26 06:57

    Firstly I think you need change title of question.

    Secondly, If you want change from String to Json just using org.json library

     JSONObject jsonObj = new JSONObject("{\"a\":\"valuea\",\"b\":\"valueb\"}");
    

    Are you tried remove. http://www.json.org/javadoc/org/json/JSONObject.html#remove(java.lang.String)

    Json.remove(key)

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