How to edit, modify nested JSONObject

前端 未结 4 1666
野趣味
野趣味 2021-02-14 21:29

Could you help me with this issue please. for example I have JSONEObject

{
\"glossary\": {
    \"title\": \"example glossary\",
    \"GlossDiv\": {
        \"ti         


        
4条回答
  •  名媛妹妹
    2021-02-14 22:01

    I found solution.

        public static JSONObject setProperty(JSONObject js1, String keys, String valueNew) throws JSONException {
        String[] keyMain = keys.split("\\.");
        for (String keym : keyMain) {
            Iterator iterator = js1.keys();
            String key = null;
            while (iterator.hasNext()) {
                key = (String) iterator.next();
                if ((js1.optJSONArray(key) == null) && (js1.optJSONObject(key) == null)) {
                    if ((key.equals(keym))) {
                        js1.put(key, valueNew);
                        return js1;
                    }
                }
                if (js1.optJSONObject(key) != null) {
                    if ((key.equals(keym))) {
                        js1 = js1.getJSONObject(key);
                        break;
                    }
                }
                if (js1.optJSONArray(key) != null) {
                    JSONArray jArray = js1.getJSONArray(key);
                    for (int i = 0; i < jArray.length(); i++) {
                        js1 = jArray.getJSONObject(i);
                    }
                    break;
                }
            }
        }
        return js1;
    }
    
    public static void main(String[] args) throws IOException, JSONException {
        FileInputStream inFile = new FileInputStream("/home/ermek/Internship/labs/java/task/test5.json");
        byte[] str = new byte[inFile.available()];
        inFile.read(str);
        String text = new String(str);
        JSONObject json = new JSONObject(text);
        setProperty(json, "rpc_server_type", "555");
        System.out.println(json.toString(4));
    

提交回复
热议问题