【问题标题】:Java: Append key value pair to nested json objectJava:将键值对附加到嵌套的 json 对象
【发布时间】:2016-10-06 11:53:56
【问题描述】:

我得到了三个输入。

  • 一个 JSON 对象(嵌套)

  • 一个节点结构

  • 键值对

我的任务是通过查看节点结构并更新原始 JSON 将键值对附加到节点。

例如,如果输入是,

  1. JSON 对象

    {
      a:
         {
           b:
              {
                c:{}
              }     
         }
    }
    
  2. 节点结构

    a.b.
    
  3. k和值v

最终更新的 JSON 应该是这样的

    {
      a:
         {
           b:
              {
                key:val
                c:{}
              }     
         }
    }

请注意,原始 JSON 也可以是 {}。然后我必须通过查看节点结构来构建整个 JSON。

这是我的代码

  • 制作键值对

     public JSONObject makeEV(String ele, String val) throws JSONException{      
       JSONObject json =new JSONObject();
       json.put(ele, val);
       return json;
     }
    
  • 将其附加到 JSON

    public void modifiedJSON(JSONObject orgJson, String nodeStruct, JSONObject ev) throws JSONException{
    JSONObject newJson = new JSONObject();
    JSONObject copyJson = newJson;
    
    char last = nodeStruct.charAt(nodeStruct.length()-1);
    String lastNode = String.valueOf(last);
    
    int i = 0; 
    while(orgJson.length() != 0 || i< nodeStruct.length()){
    
        if(orgJson.length() ==0){
            if(nodeStruct.charAt(i) == last){
                newJson.put(String.valueOf(last), ev.toString());
            }else{
                newJson.put(String.valueOf(nodeStruct.charAt(i)), "");
            }
            newJson = newJson.getJSONObject(String.valueOf(nodeStruct.charAt(i)));
    
        }
        else if(i >= nodeStruct.length()){
            if(orgJson.has(lastNode)){
                newJson.put(String.valueOf(last), ev.toString());
            }else{
    
            }
        }
    }
    }
    

    我被困在这里。请帮忙。提前致谢。

【问题讨论】:

  • 你的节点结构应该是a.b.key,不是吗?
  • 是的。那也很好

标签: java json


【解决方案1】:

可以使用String#split(regex) 来完成下一步:

public void modifiedJSON(JSONObject orgJson, String nodeStruct, 
                         String targetKey, String value)  {
    // Split the keys using . as separator 
    String[] keys = nodeStruct.split("\\.");
    // Used to navigate in the tree
    // Initialized to the root object
    JSONObject target = orgJson;
    // Iterate over the list of keys from the first to the key before the last one
    for (int i = 0; i < keys.length - 1; i++) {
        String key = keys[i];
        if (!target.has(key)) {
            // The key doesn't exist yet so we create and add it automatically  
            target.put(key, new JSONObject());
        }
        // Get the JSONObject corresponding to the current key and use it
        // as new target object
        target = target.getJSONObject(key);
    }
    // Set the last key
    target.put(targetKey, value);
}

【讨论】:

  • 其实这里的ev就是我需要插入的键值对
  • 你能做出必要的改变吗?
  • 谢谢。如果 key 作为单独的参数给出并且节点结构是 a.b. 怎么办?
  • 华丽的代码 sn-p,就像它的简短和富有表现力以及与 cmets 一样。为我节省了几个小时发明算法和搜索文档的时间 =)))
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-16
  • 2017-11-17
  • 2015-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多